我写了一些没有错误的代码。该代码使用MySQLdb进行(频繁)数据库访问,并使用4个不同的数据库。还会生成几个日志文件并使用日志记录模块。真正的担心是,当运行Valgrind时,我得到以下
==7840== LEAK SUMMARY:
==7840== definitely lost: 29 bytes in 1 blocks
==7840== indirectly lost: 0 bytes in 0 blocks
==7840== possibly lost: 1,104,793 bytes in 8,865 blocks
==7840== still reachable: 70,684 bytes in 2,194 blocks
==7840== suppressed: 0 bytes in 0 blocks
最大的泄漏是
==7840== 393,216 bytes in 1 blocks are possibly lost in loss record 1,585 of 1,585
==7840== at 0x4005903: malloc (vg_replace_malloc.c:195)
==7840== by 0x204929E: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x2054833: PyString_InternInPlace (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x20A0362: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209FB15: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x20A0075: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209FB15: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x20A0068: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x20A04CC: PyMarshal_ReadObjectFromString (in /usr/lib /libpython2.4.so.1.0)
==7840== by 0x20A1D20: PyMarshal_ReadLastObjectFromFile (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209AA63: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209CB7E: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209D9B2: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209DE71: ??? (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x209E087: PyImport_ImportModuleEx (in /usr/lib/libpython2.4.so.1.0)
==7840== by 0x207DB2D: ??? (in /usr/lib/libpython2.4.so.1.0)
所以你看,它显示了几乎1MB的内存可能会丢失。我的代码是否存在一些真正的问题,或者是使用MySQLdb来提供这样的行为。另外为了最大限度地减少它,我应该说明释放对象(包含文件,数据库连接)还是有可以查看的python模块?
答案 0 :(得分:5)
Python在malloc
之上使用自己的内存分配器,这在使用valgrind时会导致问题。有关详细说明,请参阅Misc/README.valgrind。假设您不打算重建Python,解决方案是使用Misc/valgrind-python.supp作为抑制文件,并取消注释其中禁止PyObject_Free
和PyObject_Realloc
警告的行。