[英]Python memory leaks trackdown?
Somehow the memory my Python program takes more and more memory as it runs (the VIRT and RES) column of the "top" command keep increasing. 不知何故,我的Python程序在运行时(“顶部”命令的VIRT和RES)列占用越来越多的内存。
However, I double checked my code extremely carefully, and I am sure that there is no memory leaks (didn't use any dictionary, no global variables. It's just a main method calling a sub method for a number of times). 但是,我非常小心地仔细检查了我的代码,我确信没有内存泄漏(没有使用任何字典,没有全局变量。它只是一个主方法多次调用子方法)。
I used heapy to profile my memory usage by 我使用heapy来分析我的内存使用情况
from guppy import hpy;
heap = hpy();
.....
print heap.heap();
each time the main method calls the sub method. 每次main方法调用sub方法。 Surprisingly, it always gives the same output.
令人惊讶的是,它始终提供相同的输出。 But the memory usage just keeps growing.
但内存使用量不断增长。
I wonder if I didn't use heapy right, or VIRT and RES in "top" command do not really reflect the memory my code uses? 我想知道我是不是没有使用heapy,或者“top”命令中的VIRT和RES不能真正反映我的代码使用的内存吗?
Or can anyone provide a better way to track down the memory usage in a Python script? 或者任何人都可以提供更好的方法来追踪Python脚本中的内存使用情况?
Thanks a lot! 非常感谢!
Two possible cases: 两种可能的情况:
your function is pure Python, in which case possible causes include 你的函数是纯Python,在这种情况下可能的原因包括
__del__
method, which the gc won't touch __del__
方法的对象循环,gc不会触及它 I'd suggest using the gc
module and the gc.garbage
and gc.get_objects
function (see http://docs.python.org/library/gc.html#module-gc ), to get list of existing objects, and you can then introspect them by looking at the __class__
attribute of each object for instance to get information about the object's class. 我建议使用
gc
模块和gc.garbage
以及gc.get_objects
函数(参见http://docs.python.org/library/gc.html#module-gc )来获取现有对象的列表,然后你然后可以通过查看每个对象的__class__
属性来获取它们,例如获取有关对象类的信息。
your function is at least partially written in C / C++, in which case the problem potentially is in that code. 你的函数至少部分用C / C ++编写,在这种情况下,问题可能在于该代码。 The advice above still applies, but won't be able to see all leaks: you will see leaks caused by missing calls to PY_DECREF, but not low level C/C++ allocations without a corresponding deallocation.
上面的建议仍然适用,但无法看到所有泄漏:您将看到由于缺少对PY_DECREF的调用而导致的泄漏,但没有相应的重新分配而导致的低级C / C ++分配。 For this you will need valgrind.
为此你需要valgrind。 See this question for more info on that topic
有关该主题的更多信息,请参阅此问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.