Stupid Python import memory tester
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 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