内存泄漏分析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')