內存泄漏分析html
pyrasite hookapp
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scopedom
objgraphide
objgraph.show_most_common_types()函數
http://www.cnblogs.com/xybaby/p/7183854.htmlthis
def show_backrefs()spa
生產一張有關objs的引用圖,看出看出對象爲何不釋放,後面會利用這個API來查內存泄露。debug
該API有不少有用的參數,好比層數限制(max_depth)、寬度限制(too_many)、輸出格式控制(filename output)、節點過濾(filter, extra_ignore),建議使用之間看一些document。調試
def find_backref_chain(obj, predicate, max_depth=20, extra_ignore=()):orm
找到一條指向obj對象的最短路徑,且路徑的頭部節點須要知足predicate函數 (返回值爲True)
能夠快捷、清晰指出 對象的被引用的狀況,後面會展現這個函數的威力
def show_chain():
將find_backref_chain 找到的路徑畫出來, 該函數事實上調用show_backrefs,只是排除了全部不在路徑中的節點。
profiling
pympler
muppy.print_summary()
gc
gc.isenabled()
gc.garbage
gc.get_stats()¶
Return a list of three per-generation dictionaries containing collection statistics since interpreter start. The number of keys may change in the future, but currently each dictionary will contain the following items:
gc.get_referents(*obj) 返回obj對象直接指向的對象
gc.get_objects() 返回全部被垃圾回收器(collector)管理的對象
gc.set_debug(flags)
設置調試選項,很是有用,經常使用的flag組合包含如下
gc.DEBUG_COLLETABLE: 打印能夠被垃圾回收器回收的對象
gc.DEBUG_UNCOLLETABLE: 打印沒法被垃圾回收器回收的對象,即定義了__del__的對象
gc.DEBUG_SAVEALL:當設置了這個選項,能夠被拉起回收的對象不會被真正銷燬(free),而是放到gc.garbage這個列表裏面,利於在線上查找問題
gc.DEBUG_STATS¶
Print statistics during collection. This information can be useful when tuning the collection frequency.
gc.DEBUG_COLLECTABLE
Print information on collectable objects found.
gc.DEBUG_UNCOLLECTABLE
Print information of uncollectable objects found (objects which are not reachable but cannot be freed by the collector). These objects will be added to the garbage list.
gc.DEBUG_INSTANCES
When DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print information about instance objects found.
gc.DEBUG_OBJECTS
When DEBUG_COLLECTABLE or DEBUG_UNCOLLECTABLE is set, print information about objects other than instance objects found.
gc.DEBUG_SAVEALL
When set, all unreachable objects found will be appended to garbage rather than being freed. This can be useful for debugging a leaking program.
gc.DEBUG_LEAK
The debugging flags necessary for the collector to print information about a leaking program (equal to DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE |DEBUG_INSTANCES | DEBUG_OBJECTS | DEBUG_SAVEALL).
objgraph.show_chain(objgraph.find_backref_chain(random.choice(objgraph.by_type('list')),inspect.ismodule),filename='chain.png')
import inspect, random
objgraph.show_chain(objgraph.find_backref_chain(random.choice(objgraph.by_type('dict')), inspect.ismodule), filename='chain.dot')