Summary
-------------------------
Python's GC detects cycles except if there is a set somewhere in the
cycle.
For instance.
>>> class Survivor( object ):
... def __init__( self ):
... self.s = set( (self,) )
...
>>> Survivor()
<__main__.Survivor object at 0xb7ce68ac>
>>> Survivor()
<__main__.Survivor object at 0xb79b0b0c>
>>> len( [ x for x in gc.get_objects() if isinstance( x, Survivor ) ] )
2
>>> gc.collect()
0
>>> gc.collect()
0
>>> len( [ x for x in gc.get_objects() if isinstance( x, Survivor ) ] )
2
>>> [ x for x in gc.get_objects() if isinstance( x, Survivor ) ]
[<__main__.Survivor object at 0xb7ce68ac>, <__main__.Survivor
object at 0xb79b0b0c>]
So, the Survivors survive gc.collect() !
See more complete test case below.
System infos
-------------------------
$ python
Python 2.4.1 (#1, May 14 2005, 18:44:52)
[GCC 3.3.5 (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2
>>> import gc
>>> gc.get_threshold()
(700, 10, 10)
OK, gc is compiled in, proceed...
Test Case
-------------------------
The attached program creates a new-style class and an old-style class
(of appropriate names). We create instances of these objects, each
having only a member, being a set, or a dict, or a list. Then we create
a cycle by putting the object itself inside this list, dict, or set, collect()
a few times ; cycles involving lists and dicts are gone, cycles involving
sets stay.
This was to check if it had anything to do with something else than
sets ; apparently sets are the cause.
The source is self-explanatory.
Enjoy !
|