1

我只是使用 valgrind 来分析我的应用程序的内存泄漏,因为我在长时间运行时内存(8 GB)溢出。内存使用量随着运行时间而增加。

我刚刚发现这种行为可能是有意的: Python memory leaks?

除了产生新进程来防止这种行为之外,还有什么办法吗?

我已经尝试使用 python 垃圾收集器,但没有成功: 如何在 Python 中显式释放内存?

我正在使用 Python 2.7.3 ...

手动触发垃圾收集:

3,145,728 bytes in 1 blocks are possibly lost in loss record 2,715 of 2,715
==16220==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16220==    by 0x463DA4: ??? (in /usr/bin/python2.7)
==16220==    by 0x4A1BB1: PyString_InternInPlace (in /usr/bin/python2.7)
==16220==    by 0x4AAED0: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16220==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16220==    by 0x535AE2: PyMarshal_ReadLastObjectFromFile (in /usr/bin/python2.7)
==16220==    by 0x528178: ??? (in /usr/bin/python2.7)
==16220== 
==16220== LEAK SUMMARY:
==16220==    definitely lost: 456 bytes in 10 blocks
==16220==    indirectly lost: 284 bytes in 6 blocks
==16220==      possibly lost: 3,844,678 bytes in 1,533 blocks
==16220==    still reachable: 16,937,271 bytes in 9,558 blocks

没有:

==16249== 3,145,728 bytes in 1 blocks are possibly lost in loss record 2,721 of 2,721
==16249==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16249==    by 0x463DA4: ??? (in /usr/bin/python2.7)
==16249==    by 0x4A1BB1: PyString_InternInPlace (in /usr/bin/python2.7)
==16249==    by 0x4AAED0: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AAFD6: ??? (in /usr/bin/python2.7)
==16249==    by 0x4AB0C0: ??? (in /usr/bin/python2.7)
==16249==    by 0x535AE2: PyMarshal_ReadLastObjectFromFile (in /usr/bin/python2.7)
==16249==    by 0x528178: ??? (in /usr/bin/python2.7)
==16249== 
==16249== LEAK SUMMARY:
==16249==    definitely lost: 456 bytes in 10 blocks
==16249==    indirectly lost: 284 bytes in 6 blocks
==16249==      possibly lost: 3,844,822 bytes in 1,534 blocks
==16249==    still reachable: 16,938,119 bytes in 9,558 blocks

valgrind --tool=massif 导致内存使用量增加(请参阅此链接以获取 PDF

4

1 回答 1

0

我看到两种可能的解释:

  1. 您无意中保留了对不再需要的某些对象的引用;
  2. 您低估了某些对​​象的内存占用。

解释器发生内存泄漏的可能性虽然不是不可能的,但比上述任何一种可能性都要小得多。

PS 尽管我很喜欢valgrind,但我不认为它是一个非常有用的工具来查明 Python 程序中的漏洞。

于 2013-11-13T14:46:22.467 回答