我有一个存在内存泄漏的 python 脚本,我可以使用 pympler 看到我的字典和列表对象正在增长:
from pympler import tracker
tr = tracker.SummaryTracker()
tr.print_diff()
但是我还没有想出如何在我的代码中列出有内存泄漏的变量名。有人可以帮我列出变量名和该变量名的代码用法吗?
谢谢
最佳答案
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/