Python实现内存泄露排查的示例详解

一般情况下只有需要长期运行的项目才会去关注内存的增长情况,即使是很小部分的内存泄露经过长期的运行仍然会产生很大的隐患。

python本身也是支持垃圾的自动回收的,但是在特定的情况下也是会出现内存泄露的问题的。

比如对于很多全局的列表(list)/字典(dict)等对象在经过不断的数据赋值而没有进行手动回收,或者某些对象被不停的循环引用而不能及时的进行回收等都会产生内存泄露的情况。

一般在python代码块的调试过程中会使用memory-profiler、filprofiler、objgraph等三种方式进行辅助分析,今天这里主要介绍使用objgraph对象提供的函数接口来进行内存泄露的分析。

objgraph是python的非标准模块,因此需要使用pip的方式安装一下。

pip install objgraph

更多详细的介绍可以访问下面的官方地址进行查看。

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

接下来就可以直接将objgraph导入到我们的代码块中进行使用了。

# Importing the objgraph module and renaming it to graph.
import objgraph as graph

这里初始化一组字典类型的数据对象。

dict_ = {
    '姓名': ['Python', 'Java', 'Scala'],
    '年龄': ['21', '22', '19']
}

通过objgraph.count()函数,可以统计出GC中的dict_对象的数目是多少。

# Counting the number of dict_ objects in the GC.
print(graph.count(dict_))

和objgraph.count()函数对应的是可以使用by_type返回该对象在GC中的列表,若是GC返回的为空的列表说明已经被回收了。

# Returning a list of dict_ objects in the GC.
print(graph.by_type(dict_))

在统计内存泄露时比较好用的函数就是graph.show_growth()函数,可以统计自上次调用以来增加得最多的对象。

# Showing the growth of objects in the memory since the last time it was called.
print(graph.show_growth())

# function                       3013     +3013
# tuple                          1463     +1463
# dict                           1417     +1417
# wrapper_descriptor             1178     +1178
# ReferenceType                   883      +883
# method_descriptor               814      +814
# builtin_function_or_method      794      +794
# getset_descriptor               514      +514
# type                            463      +463
# list                            436      +436
# None

可以根据返回结果中的对象每次增加的数量来判断内存泄露的相关情况。

还有一个比较常用的分析函数就是graph.show_most_common_types(),可以按照从大到小的方式列出对象实例比较多的情况。

# Showing the most common types of objects in the memory.
print(graph.show_most_common_types())

# function                   3013
# tuple                      1463
# dict                       1417
# wrapper_descriptor         1178
# ReferenceType              883
# method_descriptor          814
# builtin_function_or_method 794
# getset_descriptor          514
# type                       463
# list                       436
# None

最后一个比较使用函数就是show_backrefs函数,使用它可以分析出内存泄露的原因是什么。

它会生成一张有关objs的引用图,可以看出对象为什么不释放?只是调用该函数时的参数比较多,下面是该函数的接口。

# def show_backrefs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10,
#                   highlight=None, filename=None, extra_info=None,
#                   refcounts=False, shortnames=True, output=None,
#                   extra_node_attrs=None):

我们还是和上面一样使用dict_作为对象进行分析。

# Showing the back references of the dict_ object.
graph.show_backrefs(dict_)

执行完成后dot类型的图片已经生成了,发现出现了下面的错误,意思是没有发现支持dot的图像组件。

# Graph written to C:\Users\86159\AppData\Local\Temp\objgraph-dkqm85f0.dot (4 nodes)
# Graph viewer (xdot) and image renderer (dot) not found, not doing anything else

可以使用pip的方式分别安装graphviz xdot,这两个python的非标准模块。

pip install graphviz xdot

若是查看.dot决策树图像可以使用graphviz工具,可以到下面地址进行下载安装。

https://graphviz.org/download/

安装完成后配置环境变量,然后重启开发工具(这里使用的是pycharm)即可。

关于Python实现内存泄露排查的示例详解的文章就介绍至此,更多相关Python内存泄露排查内容请搜索编程宝库以前的文章,希望以后支持编程宝库

今天继续给大家介绍Python相关知识,本文主要内容是Python asyncio异步编程简单实现。 一、asyncio事件循环简介asyncio引入了事件循环的概念。事件循环是一个 ...