This document describes some caveats about the use of Valgrind withPython. Valgrind is used periodically by Python developers to tryto ensure there are no memory leaks or invalid memory reads/writes.If you want to enable valgrind support in Python, you will need toconfigure Python --with-valgrind option or an older option--without-pymalloc.UPDATE: Python 3.6 now supports PYTHONMALLOC=malloc environment variable whichcan be used to force the usage of the malloc() allocator of the C library.If you don't want to read about the details of using Valgrind, thereare still two things you must do to suppress the warnings. First,you must use a suppressions file. One is supplied inMisc/valgrind-python.supp. Second, you must do one of the following:* Uncomment Py_USING_MEMORY_DEBUGGER in Objects/obmalloc.c,then rebuild Python* Uncomment the lines in Misc/valgrind-python.supp thatsuppress the warnings for PyObject_Free and PyObject_ReallocIf you want to use Valgrind more effectively and catch even morememory leaks, you will need to configure python --without-pymalloc.PyMalloc allocates a few blocks in big chunks and most objectallocations don't call malloc, they use chunks doled about by PyMallocfrom the big blocks. This means Valgrind can't detectmany allocations (and frees), except for those that are forwardedto the system malloc. Note: configuring python --without-pymallocmakes Python run much slower, especially when running under Valgrind.You may need to run the tests in batches under Valgrind to keepthe memory usage down to allow the tests to complete. It seems to takeabout 5 times longer to run --without-pymalloc.Apr 15, 2006:test_ctypes causes Valgrind 3.1.1 to fail (crash).test_socket_ssl should be skipped when running valgrind.The reason is that it purposely uses uninitialized memory.This causes many spurious warnings, so it's easier to just skip it.Details:--------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 disablePyMalloc when configuring python by adding the --without-pymalloc option.If you disable PyMalloc, most of the information in this document andthe supplied suppressions file will not be useful. As discussed above,disabling PyMalloc can catch more problems.PyMalloc uses 256KB chunks of memory, so it can't detect anythingwrong within these blocks. For that reason, compiling Python--without-pymalloc usually increases the usefulness of other tools.If you use valgrind on a default build of Python, you will seemany 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. Tim Peters explainsthe situation:PyMalloc needs to know whether an arbitrary address is onethat's managed by it, or is managed by the system malloc.The current scheme allows this to be determined in constanttime, regardless of how many memory areas are under pymalloc'scontrol.The memory pymalloc manages itself is in one or more "arenas",each a large contiguous memory area obtained from malloc.The base address of each arena is saved by pymallocin a vector. Each arena is carved into "pools", and a field atthe start of each pool contains the index of that pool's arena'sbase address in that vector.Given an arbitrary address, pymalloc computes the pool baseaddress corresponding to it, then looks at "the index" storednear there. If the index read up is out of bounds for thevector of arena base addresses pymalloc maintains, thenpymalloc knows for certain that this address is not underpymalloc's control. Otherwise the index is in bounds, andpymalloc comparesthe arena base address stored at that index in the vectortothe arbitrary address pymalloc is investigatingpymalloc controls this arbitrary address if and only if it liesin the arena the address's pool's index claims it lies in.It doesn't matter whether the memory pymalloc reads up ("theindex") is initialized. If it's not initialized, thenwhatever trash gets read up will lead pymalloc to conclude(correctly) that the address isn't controlled by it, eitherbecause the index is out of bounds, or the index is in boundsbut the arena it represents doesn't contain the address.This determination has to be made on every call to one ofpymalloc's free/realloc entry points, so its speed is critical(Python allocates and frees dynamic memory at a ferocious rate-- everything in Python, from integers to "stack frames",lives in the heap).
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。