mitmproxy/test/helper_tools/memoryleak.py

46 lines
1.2 KiB
Python
Raw Normal View History

2015-11-26 22:26:01 +00:00
import gc
import threading
2022-11-29 13:28:41 +00:00
2015-11-26 22:26:01 +00:00
from OpenSSL import SSL
2022-11-29 13:28:41 +00:00
from pympler import muppy
from pympler import refbrowser
2022-04-26 11:53:35 +00:00
2015-11-26 22:26:01 +00:00
# import os
# os.environ["TK_LIBRARY"] = r"C:\Python27\tcl\tcl8.5"
# os.environ["TCL_LIBRARY"] = r"C:\Python27\tcl\tcl8.5"
2015-11-26 22:26:01 +00:00
# Also noteworthy: guppy, objgraph
step = 0
__memory_locals__ = True
def str_fun(obj):
if isinstance(obj, dict):
if "__memory_locals__" in obj:
return "(-locals-)"
if "self" in obj and isinstance(obj["self"], refbrowser.InteractiveBrowser):
return "(-browser-)"
2022-04-26 11:53:35 +00:00
return (
str(id(obj))
+ ": "
+ str(obj)[:100].replace("\r\n", "\\r\\n").replace("\n", "\\n")
)
2015-11-26 22:26:01 +00:00
def request(ctx, flow):
global step, ssl
print("==========")
print(f"GC: {gc.collect()}")
print(f"Threads: {threading.active_count()}")
2015-11-26 22:26:01 +00:00
step += 1
if step == 1:
all_objects = muppy.get_objects()
ssl = muppy.filter(all_objects, SSL.Connection)[0]
if step == 2:
ib = refbrowser.InteractiveBrowser(ssl, 2, str_fun, repeat=False)
del ssl # do this to unpollute view
ib.main(True)
2016-01-27 09:12:18 +00:00
# print("\r\n".join(str(x)[:100] for x in gc.get_referrers(ssl)))