Skip to main content
 首页 » 编程设计

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

2024年10月24日10TianFang

我有一个存在内存泄漏的 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% 

这是你要找的吗?