__subclasses__() (was OK, now *this* is cool)

Ype Kingma ykingma at accessforall.nl
Wed Dec 12 13:22:01 EST 2001


Andrew Dalke wrote:
> 
> Ype Kingma:
> > Jython leaves all it's garbage collection to the Java VM.
> > This simplifies the Jython source code (a lot, no reference
> > counting anywhere). On the downside it is more difficult
> > to have a method executed when an object becomes garbage.
> 
> "more difficut" means it's still possible.  How?
> 
> I have a Python wrapper on top of a C library.  I use
> __del__ to call the special deallocator for the library
> when the object is no longer needed.
> 
> The library also has bindings for use with Java.  I tried
> using JPython (it was a while ago) but got memory leaks
> all over the place because it doesn't call __del__ methods;
> prefering the Java garbage collector.
> 
> How do I make Jython work with that wrapper?  try/finally
> doesn't work for my needs.
> 

I had another look at the jython sources.
Since just over a year it will call your
__del__() method when it is present at object instantiation time,
ie. when you have a 'def __del__(self):' in your class.
It's implemented by calling from an overriden 'java.lang.Object.finalize()'.
This means that __del__() can be called after the object has been
identified as garbage by the JVM. The call is not guaranteed
at all, but it will eventually be called under memory pressure
when the JVM does not exit beforehand and there is some CPU time
to spare.
It leaves you at the mercy of the garbage collector of the JVM, but
this is probably the best thing that can be done in jython.
It is also very python-esque in that it uses what happens to be
available in the environment.

This means there is no need for the (untested) code of my previous
post.

Have fun,
Ype



More information about the Python-list mailing list