无法找到 python 中内存泄漏的位置 Tracemalloc & Guppy & mem_top & pympler



这四个包已经检测到python占用了大约400MB,但当我使用"top-d1"时,它几乎占用了3GB!有没有更精确的方法来找出内存泄漏的位置?

更多信息如下:

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND   
15881 root      20   0 5206548   3.1g  32960 S   0.0   4.9 334:13.48 python3   

types |   # objects |   total size
============================= | =========== | ============
dict |      653590 |    234.98 MB
str |      791447 |    116.25 MB
guppy.sets.setsc.ImmNodeSet |         895 |     47.54 MB
int |     1286323 |     36.75 MB
tuple |      167646 |     14.07 MB
list |        5116 |      6.03 MB
bytes |       77163 |      5.50 MB
code |       38540 |      5.32 MB
type |        5814 |      5.31 MB
set |        2039 |    959.21 KB
weakref |       10108 |    868.66 KB
abc.ABCMeta |         408 |    417.58 KB
getset_descriptor |        5273 |    411.95 KB
cell |        7029 |    384.40 KB
function (__init__) |        2519 |    354.23 KB

哦,天哪,内存泄漏很难处理

假设这是linux系统,也许可以从以下内容开始,看看是否可以隔离大内存区域(PID15881(

sudo pmap 15881

假设你识别了一个大面积的

..
0000558f3f556000 4507556K rw---   [ anon ]
..

获取范围

sudo cat /proc/15881/maps

例如

558f3f556000-559052762000 rw-p 00000000 00:00 0     [heap]

使用gdb将该区域附加并转储到文件

sudo gdb --pid 15881
(gdb) dump memory /tmp/mem.dump 0x558f3f556000 0x559052762000

浏览/tmp/mem.dump文件-检查频繁出现的部件

strings /tmp/mem.dump

害怕需要熟悉你的python应用程序的人来计算

最新更新