python - 使用pympler识别哪些变量在python中泄漏内存

标签 python memory

我有一个存在内存泄漏的 python 脚本,我可以使用 pympler 看到我的字典和列表对象正在增长:

from pympler import tracker
tr = tracker.SummaryTracker()
tr.print_diff() 

但是我还没有想出如何在我的代码中列出有内存泄漏的变量名。有人可以帮我列出变量名和该变量名的代码用法吗?

谢谢

最佳答案

来自 Pympler's documentation :

Tracking the lifetime of objects of certain classes can be achieved with the Class Tracker. This gives insight into instantiation patterns and helps to understand how specific objects contribute to the memory footprint over time:

>>> from pympler import classtracker
>>> tr = classtracker.ClassTracker()
>>> tr.track_class(Document)
>>> tr.create_snapshot()
>>> create_documents()
>>> tr.create_snapshot()
>>> tr.stats.print_summary()
              active      1.42 MB      average   pct
   Document     1000    195.38 KB    200     B   13%

这是你要找的吗?

关于python - 使用pympler识别哪些变量在python中泄漏内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472161/

相关文章:

c++ - 是否需要在原始指针位置调用delete[]

c - 获取内存在 NUMA 中的位置

php - 合并 2 个非常大的文本文件,更新每一行,不使用内存

linux - 如何走内核的VMA?

python - 在 Windows 7 上安装 Google Cloud SDK 时出错

python - 使用导入文件中的变量给出名称错误,即使已使用 'global'

python - 查看python中所有继承的类

python - 正则表达式中的可选括号

python - 如果为空,则 append 到 DataFrame 时出现问题

c - 动态分配的字符串内存数组似乎不起作用