public
Created

Simplistic demonstration of Python GC behaviour

  • Download Gist
listref.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import gc
import weakref
 
class Object(object): pass
 
l = [Object()]
l[0].l = l
 
REFS = weakref.WeakKeyDictionary()
REFS[l[0]] = True
 
del l
 
print (list(REFS.keys()))
 
gc.collect()
 
print (list(REFS.keys()))

Output in both Python 2.6.6 and 3.2:

[<__main__.Object object at 0x258d690>]
[]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.