python内存泄露排查

相关工具

gc

import gc

gc.set_debug(False)
gc.get_referrers(obj)
gc.garbage

grappy

from guppy import hpy

h=hpy()

h.heap()

objgraph

# https://mg.pov.lt/objgraph/
import objgraph

objgraph.show_most_common_types(limit=10,shortnames=False)
objgraph.show_growth(limit=10)
objgraph.by_type("http.cookiejar.Cookie")
a = objgraph.by_type("builtins.dict")[20000]
objgraph.show_backrefs(objgraph.by_type('OBJ')[0], max_depth = 10, filename = 'obj.dot')
objgraph.show_backrefs(a, max_depth = 3, filename = '/data/msc/msc/crawlers/shopify/obj.dot')

tracemalloc

import tracemalloc

tracemalloc.start(25)

snapshot1 = tracemalloc.take_snapshot()
snapshot2 = tracemalloc.take_snapshot()

top_stats = snapshot1.compare_to(snapshot2, 'lineno')

print(top_stats)

prasite

yum install gdb python-debug

prasite-shell <PID>
timed out 问题解决:

site-packages/pyrasite/ipc.py

将settimeout默认5改成50
# 查看线程数
len(threading.enumerate())
threading.activeCount()

pympler

from pympler import tracker
tr = tracker.SummaryTracker()
tr.print_diff()

graphviz

dot -Tpng xxx.dot -o xxx.png
dot.exe -Tjpg downloader.dot -o downloader.jpg

filprofiler

pip install filprofiler

fil-profile run oom.py 

sys

sys.getsizeof(obj)

sys.getrefcount(obj)
tag(s):
show comments · back · home