Python内存泄漏? [英] Python memory leaks?

查看:75
本文介绍了Python内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个python扩展,似乎正在泄漏内存.我正在尝试使用valgrind找出问题的根源.

I am writing a python extension that seems to be leaking memory. I am trying to figure out the soure of the problem using valgrind.

但是,根据valgrind的说法,python本身似乎正在泄漏内存.使用以下简单脚本:

However, it seems that python itself is leaking memory according to valgrind. Using the following simple script:

hello.py

  print "Hello World!"

然后做

> valgrind --tool=memcheck python ./hello.py

(...)
==7937== ERROR SUMMARY: 580 errors from 34 contexts (suppressed: 21 from 1)
==7937== malloc/free: in use at exit: 721,878 bytes in 190 blocks.
==7937== malloc/free: 2,436 allocs, 2,246 frees, 1,863,631 bytes allocated.
==7937== For counts of detected errors, rerun with: -v
==7937== Use --track-origins=yes to see where uninitialised values come from
==7937== searching for pointers to 190 not-freed blocks.
==7937== checked 965,952 bytes.
==7937== 
==7937== LEAK SUMMARY:
==7937==    definitely lost: 0 bytes in 0 blocks.
==7937==      possibly lost: 4,612 bytes in 13 blocks.
==7937==    still reachable: 717,266 bytes in 177 blocks.
==7937==         suppressed: 0 bytes in 0 blocks.
==7937== Rerun with --leak-check=full to see details of leaked memory.

有人对此种行为有解释吗? python解释器真的泄漏内存吗?

Does anybody have an explanation for this strage behavior? Is the python interpreter really leaking memory?

python开发人员使用什么工具调试其内存泄漏?

What tool do python developers use to debug their memory leaks?

推荐答案

Python源码中有一个完整的README.valgrind,它解释了尝试将Valgrind与Python结合使用的各种注意事项:

There's a whole README.valgrind in the Python sources that explains the various caveats trying to use Valgrind with Python:

http://svn.python.org/projects/python/trunk/Misc/README.valgrind

Python uses its own small-object allocation scheme on top of malloc,
called PyMalloc.

Valgrind may show some unexpected results when PyMalloc is used.
Starting with Python 2.3, PyMalloc is used by default.  You can disable
PyMalloc when configuring python by adding the --without-pymalloc option.
If you disable PyMalloc, most of the information in this document and
the supplied suppressions file will not be useful.  As discussed above,
disabling PyMalloc can catch more problems.

If you use valgrind on a default build of Python,  you will see
many errors like:

        ==6399== Use of uninitialised value of size 4
        ==6399== at 0x4A9BDE7E: PyObject_Free (obmalloc.c:711)
        ==6399== by 0x4A9B8198: dictresize (dictobject.c:477)

These are expected and not a problem. 

这篇关于Python内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆