This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cycle containing a Set is not GC'd [leak]
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: peufeu, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2005-08-25 22:20 by peufeu, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
gc_cycles.py peufeu, 2005-08-25 22:23 Test program
Messages (4)
msg26120 - (view) Author: Peufeu (peufeu) Date: 2005-08-25 22:20
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 !


msg26121 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2005-08-25 22:25
Logged In: YES 
user_id=31435

Assigned to Raymond.  Is this a duplicate of

http://www.python.org/sf/1200018

?
msg26122 - (view) Author: Peufeu (peufeu) Date: 2005-08-25 23:09
Logged In: YES 
user_id=587274

Looks like it's a dupe. I did not see it in the release notes. Is it fixed in a 
recently released version ? It's a rather annoying bug, caught a server 
process hogging all the RAM and swap today because of this one.
msg26123 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-08-26 03:53
Logged In: YES 
user_id=80475

This is a duplicate report.
Was fixed for 2.4.2. which should be out in September.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42305
2005-08-25 22:20:12peufeucreate