Skip to content

Instantly share code, notes, and snippets.

@ssanderson
Created July 18, 2014 20:15
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 ssanderson/33d4eec82eb84e1e3117 to your computer and use it in GitHub Desktop.
Save ssanderson/33d4eec82eb84e1e3117 to your computer and use it in GitHub Desktop.
Minimal refcycle example
import gc
import objgraph
class Foo(object):
def __init__(self):
self._gen = None
def __iter__(self):
self._gen = self.gen()
return self._gen
def gen(self):
try:
big_list = [i for i in xrange(1000)]
for i in big_list:
yield i
except:
raise
def foo():
count = 0
while count < 10:
count += 1
print count
f = Foo()
it = iter(f)
it.next()
if __name__ == "__main__":
gc.collect()
gc.collect()
foo()
gc.collect()
gc.collect()
objgraph.show_backrefs(objgraph.by_type('Foo'), max_depth=10, filename="results_with_try_except.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment