python的内存调试
2019-07-15 本文已影响0人
狼无雨雪
python内存增加,内存泄漏调试
gc,objgraph
### 强制进行垃圾回收
gc.collect()
# ### 打印出对象数目最多的 50 个类型信息
# objgraph.show_most_common_types(limit=20)
objgraph.show_growth()
# objgraph.show_backrefs(objgraph.by_type('function')[0], max_depth = 10, filename = 'obj.dot')
# objgraph.show_chain(
# objgraph.find_backref_chain(
# objgraph.by_type('_InputList')[0],
# objgraph.is_proper_module
# ),
# filename='obj_chain.dot')
# objgraph.show_backrefs(objgraph.by_type('Tensor')[0], extra_ignore=(id(gc.garbage),), max_depth = 10, filename = 'del_obj.dot')
以此方法生成dot文件之后
import os
import pydotplus
import pydot
os.environ["PATH"] += os.pathsep + 'D:/graphviz/bin'
file_path = "D:/microsoft/ImageCreation/del_obj.dot"
output_path = "D:/microsoft/ImageCreation/del_obj.pdf"
with open(file_path,'r') as f:
graph = pydot.graph_from_dot_data(f.read())
graph[0].write_pdf(output_path)
用pydot将dot转换成pdf可视化观看
另外一种方式,木有尝试过,可能比较简单,直接保存为graph.png格式,省略转换步骤
import objgraph
objgraph.show_growth() # show the growth of the objects
objgraph.show_refs(variableName, filename='graph.png') # show the reference structure of the variable