Skip to content

anonymous /The hunt for Pythons memory leaks.py
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import gc
import sys
sm = sys.modules.copy()
print len(gc.get_objects()) # 4075
import httplib
print len(gc.get_objects()) # 7064
sys.modules.pop('httplib')
gc.collect()
print len(gc.get_objects()) # 7064
del httplib
gc.collect()
print len(gc.get_objects()) # 6747
sys.modules = sm
gc.collect()
print len(gc.get_objects()) # 6747
globals().clear()
import gc
gc.collect()
print len(gc.get_objects()) # 6747
locals().clear()
import gc
gc.collect()
print len(gc.get_objects()) # 6747
g = {}
exec 'import httplib' in g
del g
gc.collect()
print len(gc.get_objects()) # back to 7064
from types import ModuleType
import sys
print len(gc.get_objects()) # 7064
sys.modules['httplib'] = ModuleType('httplib')
print len(gc.get_objects()) # 7066
for key in sys.modules.keys():
del sys.modules[key]
gc.collect()
print len(gc.get_objects()) # 7064
import httplib
for attr in dir(httplib):
setattr(httplib, attr, None)
gc.collect()
print len(gc.get_objects()) # 6749
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.