Python vs Java garbage collection?

Martin Maney maney at pobox.com
Wed Dec 25 00:27:54 EST 2002


"Martin v. L?wis" <martin at v.loewis.de> wrote:
> Bengt Richter wrote:
>> E.g., suppose you had a suite-introducer whose purpose it was to define
>> a local block of statements like try/finally but so that the compiler
>> would automatically generate code so that all bindings created within
>> the suite would be monitored and and immediately (i.e., maybe even
>> during expression evaluation, but at least at the end of a statement)
>> finalized when the equivalent of ref count going to zero happened. E.g.,
>>       autofinalize:
>>             s = file('foo').read()
> 
> How would you implement this in Jython? In particular, what would be the 
> equivalent of ref count, and how could you tell it goes to zero?

It seems to me that it would most likely be a reference count.  I take
it Jython doesn't use reference counting, leaving the entire job to the
Java runtime (with a hook to run Python finalizers, of course)?

Hmmm.  So presumably Jython wouldn't care to add ref counting to
everything, but it would be useful to be able to get that behavior for
selected objects (or types of objects) both to avoid a potential
portability problem amongst Python implementations as well as to insure
that this useful behavior was always available.  Perhaps it would be
useful to have a way of activating ref counting for an object.  In
CPython this would be a no-op; in Jython it would have to add ref
counting to the chosen objects and call finalizers, but would not [need
not?] scavenge memory when the count hit zero.

So the language would guarantee that objects for which ref counting was
selected (hmmm, has to be active at creation time, yes?  certainly can't
have multiple bindings when this is turned on) would have finalizers
run when their ref count went to zero.  Other objects might or might
not, and cycles would likely continue to be troublesome.  This would
address at least most of the uses I've seen described here, I think.

Okay, start poking holes.

Merry Christmas!




More information about the Python-list mailing list