Python vs Java garbage collection?

Martin Maney maney at pobox.com
Fri Dec 27 12:00:22 EST 2002


Martin v. L?wis <martin at v.loewis.de> wrote:
> "Martin Maney" <maney at pobox.com> writes:
>> It seems to me that it would most likely be a reference count.  
> 
> But that would slow down assignments and parameter passing in

Yes, I assumed that that was at least a good part of the reason Jython
abandonded the traditional ref counting of CPython.  This was also
meant to be implicit agreement with your perhaps rhetorical question to
the previous poster.  I can't see any way to provide CPython's
finalizer semantics other than by doing ref counting.  The rest of my
post was speculation about whether ref counting might be applied to
selected objects (or types) as a compromise between the convenince of
ref counting's determinism and its cost if applied to every object.

> inacceptable ways, and you would also need to accomodate for the case
> where Java objects hold references to Python objects.

Now that would be a problem.  I'm not familiar with Jython, so I have
no idea how common that sort of use is, or whether restricting
"guaranteed to finalize like classic CPython objects" objects from
being so referenced would be feasible.  My impression, which may be
incomplete or even just wrong, is that Jython's motivation was mostly
to allow a Python language implementation that ran on top of the Java
VM, and which could access the Java libraries.  In that context it
doesn't seem impossible that such an extension might be worth accepting
some such restrictions.  Dunno.

I was also thinking that something along these lines might also be
useful for other hypothetical implementations.  Even accepting that
CPython will never, ever use anything but ref counting (aside from
appendages that attempt to deal with cyclic garbage <wink>), there
might come to be other implementations that would find a different
approach to garbage collection profitable.  If they can compile Lisp
into machine code, why not Python?  And if that makes the ref counting
a less insignificant cost and favors some other approach to gc...

>> Perhaps it would be useful to have a way of activating ref counting
>> for an object.
> 
> Now, how would you implement *that*? I.e. when activated, which
> machinery precisely changes the reference count, on, say, assignment?

Dunno.  I have been enjoying Python as a tool without so far having had
any sufficent motivation to open the hood and see how the plumbing
works; nor would I be likely to begin by examining Jython's plumbing. 
So I am extrapolating from past experiences with using ref counting
in other contexts to an assumption that Python's implementation might
be able to choose an answer other than "all" or "nothing".

May I reflect an altered version of this back to you?  Under the
hypothesis that CPython changed so that not all objects were reference
counted, how many places in the VM would need to distinguish between
ref-counted and non-rc objects?  Would it be easier to have the
distinction made based on the object's type or at a lower level such as
a sentinel ref count value?  I don't know how directly the answers to
these questions would translate to Jython's implementation, of course.




More information about the Python-list mailing list