Python vs Java garbage collection?

Donn Cave donn at u.washington.edu
Mon Dec 23 14:07:41 EST 2002


Quoth alloydflanagan at attbi.com (A. Lloyd Flanagan):
| "Martin v. Löwis" <martin at v.loewis.de> wrote in message news:<au75tt$jd0$04$1 at news.t-online.com>...
|> Robert Oschler wrote:
|> > A big hearty agreement. I've been pushing the "destructors please" issue for
|> > quite some time too.  
|> 
|> Can you please explain what a destructor is? If it is a fragment of code 
|> executed when an object ends its life, what exactly is it that you are 
|> pushing? since both Python and Java have such a feature.
|
| A destructor is like a finalization method, it only gets called when
| all references to the object are gone.  The difference between C++
| destructors and Java's finalize() or Python's __del__() is that C++
| _guarantees_ that the destructor will get called sometime before the
| program exits (barring a core dump or other catastrophe).  This turns
| out to be terribly useful for things like making sure an external
| resource gets cleaned up when it's not needed, and not before.
|
| From the Python reference manual: "It is not guaranteed that __del__()
| methods are called for objects that still exist when the interpreter
| exits."
|
| So I think what he's saying is that he wants a guarantee that
| __del__() gets called at some point, and I agree with him.

As I understand it, what's really missing here is that you can't expect
__del__() to be invoked when an object is reclaimed from a cycle by
Python's gc.  Or rather, you can expect it not to be called.  I don't
know for sure that gc actually runs at program exit, but even if it did,
__del__ won't be called because of potential order dependencies.

If A and B escape reference count collection because they refer to each
other, then when they're finally located by gc, will A.__del__ expect
to find B in working condition - what if it's already been collected?

Does your destructor account for this problem?

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list