内存泄漏分析
内存泄漏分析
pyrasite hook
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
objgraph
objgraph.show_most_common_types()
http://www.cnblogs.com/xybaby/p/7183854.html
def show_backrefs()
生产一张有关objs的引用图,看出看出对象为什么不释放,后面会利用这个API来查内存泄露。
该API有很多有用的参数,比如层数限制(max_depth)、宽度限制(too_many)、输出格式控制(filename output)、节点过滤(filter, extra_ignore),建议使用之间看一些document。
def find_backref_chain(obj, predicate, max_depth=20, extra_ignore=()):
找到一条指向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:
- ·collections is the number of times this generation was collected;
- ·collected is the total number of objects collected inside this generation;
- ·uncollectable is the total number of objects which were found to be uncollectable (and were therefore moved to the garbage list) inside this generation.
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')