Skip to content

Instantly share code, notes, and snippets.

@justecorruptio
Created September 29, 2017 18:24
Show Gist options
  • Save justecorruptio/1d310c5448e4ffc54e4874095e792874 to your computer and use it in GitHub Desktop.
Save justecorruptio/1d310c5448e4ffc54e4874095e792874 to your computer and use it in GitHub Desktop.
Stupid Python import memory tester
import gc
import os
import sys
import time
import imp
import ihooks
__PID = os.getpid()
def rss():
#gc.collect()
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()
try:
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
))
finally:
__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