Final garbage collection?

Jerome Quelin jerome.quelin at insalien.org
Mon Jun 26 10:11:43 EDT 2000


Hi all,

I would like to know if there is really a final garbage-collection in
Python: it seems that it does not handle cross-references if we do not
break cycles...

$ cat test.py
#!/usr/bin/python
class Test:
    def __init__(self, name):
        self.name = name
    def __del__(self):
        print "Ouch! (", self.name, ")"
a = Test('a')
b = Test('b')
c = Test('c')
a.cross_ref = b
b.cross_ref = a
$ ./test.py
Ouch! ( c )
$

It seems like only the c instance gets destroyed?!
Yet when the interpreter stops, it is supposed to give all the memory
back to the system, therefore destroying all objects created during
the run of the program. And if the objects are destroyed, aren't the
destructors supposed to be called?

But in the example shown above, the only instance for which the
destructor gets called is 'c'. What happened to the 'a' and 'b'
instances? They sure are destroyed by the system, but does Python
notice this "memory leak" (ok, it isn't a memory leak since the system
gets the memory back)? Why do their destructors don't get called? Is
it that Python does not have a _final_ garbage-collection that can
handle cycles and break them?

Thanks,
Jerome
--
jerome.quelin at insalien.org



More information about the Python-list mailing list