Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2010 21:47
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 anonymous/450606 to your computer and use it in GitHub Desktop.
Save anonymous/450606 to your computer and use it in GitHub Desktop.
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