Skip to content

Instantly share code, notes, and snippets.

Created September 29, 2017 18:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Stupid Python import memory tester
import gc
import os
import sys
import time
import imp
import ihooks
__PID = os.getpid()
def rss():
return int(os.popen('ps -o rss --noheaders %s' % (__PID)).read())
__old_import__ = __builtins__['__import__']
__indent = -1
def __new__import__(name, globals=None, locals=None, fromlist=None, level=-1):
global __indent
__indent += 1
if __indent < 10:
before = rss()
mod = __old_import__(name, globals=globals, locals=locals, fromlist=fromlist, level=level)
if __indent < 10:
after = rss()
if after - before > 50:
sys.stderr.write("%s[%s] %s\n" % (
' ' * __indent, name, after - before
__indent -= 1
return mod
__builtins__['__import__'] = __new__import__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment