Skip to content

Instantly share code, notes, and snippets.

@gvx
Created October 3, 2012 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvx/3830294 to your computer and use it in GitHub Desktop.
Save gvx/3830294 to your computer and use it in GitHub Desktop.
Simplistic demonstration of Python GC behaviour
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()))
@gvx
Copy link
Author

gvx commented Oct 3, 2012

Output in both Python 2.6.6 and 3.2:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment