Created
July 18, 2014 20:15
-
-
Save ssanderson/33d4eec82eb84e1e3117 to your computer and use it in GitHub Desktop.
Minimal refcycle example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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