[Python-Dev] weak refs and jython

Samuele Pedroni pedroni@inf.ethz.ch
Wed, 31 Jan 2001 23:29:37 +0100


Hi.

[Fred L. Drake, Jr.]

>  > Java weak refs cannot be resurrected.
>
>   This is certainly annoying.
>   How about this: the callback receives the weak reference object or
> proxy which it was registered on as a parameter.  Since the reference
> has already been cleared, there's no way to get the object back, so we
> don't need to get it from Java either.
>   Would that be workable?  (I'm adjusting my patch now.)

Yes, it is workable: clearly we can implement weak refs only under java2 but
this is not (really) an issue.
We can register the refs in a java reference queue, and poll it lazily or
trough a low-priority thread
in order to invoke the callbacks.

-- Some remarks
I have used java weak/soft refs to implement some of the internal tables of
jython in order to avoid memory leaks, at least
under java2.

I imagine that the idea behind callbacks plus resurrection was to enable the
construction of sofisticated caches.

My intuition is that these features are not present under java because they
will interfere too much with gc
and have a performance penalty.
On the other hand java offers reference queues and soft references, the latter
cover the common case of caches
that should be cleared when there is few memory left. (Never tried them
seriously, so I don't know if the
actual impl is fair, or will just wait too much starting to discard things =>
behavior like primitives gc).

The main difference I see between callbacks and queues approach is that with
queues is this left to the user
when to do the actual cleanup of his tables/caches, and handling queues
internally has a "low" overhead.
With callbacks what happens depends really on the collection times/patterns and
the overhead is related
to call overhead and how much is non trivial, what the user put in the
callbacks. Clearly general performance
will not be easily predictable.
(From a theoretical viewpoint one can simulate more or less queues with
callbacks and the other way around).

Resurrection makes few sense with queues, but I can easely see that lacking of
both resurrection and soft refs
limits what can be done with weak-like refs.

Last thing: one of the things that is really missing in java refs features is
that one cannot put conditions of the form
as long A is not collected B should not be collected either. Clearly I'm
referring to situation when one cannot modify
the class of A in order to add a field, which is quite typical in java. This
should not be a problem with python and
its open/dynamic way-of-life.

regards, Samuele Pedroni.