blob: 157bdc3ca3a1680e78f3e8496e03249e9c27d057 [file] [log] [blame]
Neal Norwitzc3cd9df2004-06-06 19:58:40 +00001This document describes some caveats about the use of Valgrind with
Tim Petersb8b20e22004-07-07 02:46:03 +00002Python. Valgrind is used periodically by Python developers to try
Neal Norwitzc3cd9df2004-06-06 19:58:40 +00003to ensure there are no memory leaks or invalid memory reads/writes.
4
5If you don't want to read about the details of using Valgrind, there
Tim Petersb8b20e22004-07-07 02:46:03 +00006are still two things you must do to suppress the warnings. First,
Neal Norwitzc3cd9df2004-06-06 19:58:40 +00007you must use a suppressions file. One is supplied in
8Misc/valgrind-python.supp. Second, you must do one of the following:
9
10 * Uncomment Py_USING_MEMORY_DEBUGGER in Objects/obmalloc.c,
Tim Petersb8b20e22004-07-07 02:46:03 +000011 then rebuild Python
12 * Uncomment the lines in Misc/valgrind-python.supp that
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000013 suppress the warnings for PyObject_Free and PyObject_Realloc
14
Neal Norwitz7bcabc62005-11-20 23:58:38 +000015If you want to use Valgrind more effectively and catch even more
16memory leaks, you will need to configure python --without-pymalloc.
17PyMalloc allocates a few blocks in big chunks and most object
18allocations don't call malloc, they use chunks doled about by PyMalloc
19from the big blocks. This means Valgrind can't detect
20many allocations (and frees), except for those that are forwarded
21to the system malloc. Note: configuring python --without-pymalloc
22makes Python run much slower, especially when running under Valgrind.
23You may need to run the tests in batches under Valgrind to keep
24the memory usage down to allow the tests to complete. It seems to take
25about 5 times longer to run --without-pymalloc.
26
27
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000028Details:
29--------
Tim Petersb8b20e22004-07-07 02:46:03 +000030Python uses its own small-object allocation scheme on top of malloc,
31called PyMalloc.
32
33Valgrind may show some unexpected results when PyMalloc is used.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000034Starting with Python 2.3, PyMalloc is used by default. You can disable
35PyMalloc when configuring python by adding the --without-pymalloc option.
Tim Petersb8b20e22004-07-07 02:46:03 +000036If you disable PyMalloc, most of the information in this document and
Neal Norwitz7bcabc62005-11-20 23:58:38 +000037the supplied suppressions file will not be useful. As discussed above,
38disabling PyMalloc can catch more problems.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000039
40If you use valgrind on a default build of Python, you will see
41many errors like:
42
43 ==6399== Use of uninitialised value of size 4
44 ==6399== at 0x4A9BDE7E: PyObject_Free (obmalloc.c:711)
45 ==6399== by 0x4A9B8198: dictresize (dictobject.c:477)
46
47These are expected and not a problem. Tim Peters explains
48the situation:
49
50 PyMalloc needs to know whether an arbitrary address is one
Tim Petersb8b20e22004-07-07 02:46:03 +000051 that's managed by it, or is managed by the system malloc.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000052 The current scheme allows this to be determined in constant
53 time, regardless of how many memory areas are under pymalloc's
54 control.
55
56 The memory pymalloc manages itself is in one or more "arenas",
Tim Petersb8b20e22004-07-07 02:46:03 +000057 each a large contiguous memory area obtained from malloc.
58 The base address of each arena is saved by pymalloc
59 in a vector. Each arena is carved into "pools", and a field at
60 the start of each pool contains the index of that pool's arena's
61 base address in that vector.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000062
Tim Petersb8b20e22004-07-07 02:46:03 +000063 Given an arbitrary address, pymalloc computes the pool base
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000064 address corresponding to it, then looks at "the index" stored
65 near there. If the index read up is out of bounds for the
66 vector of arena base addresses pymalloc maintains, then
67 pymalloc knows for certain that this address is not under
68 pymalloc's control. Otherwise the index is in bounds, and
69 pymalloc compares
70
71 the arena base address stored at that index in the vector
72
73 to
74
Tim Petersb8b20e22004-07-07 02:46:03 +000075 the arbitrary address pymalloc is investigating
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000076
Tim Petersb8b20e22004-07-07 02:46:03 +000077 pymalloc controls this arbitrary address if and only if it lies
78 in the arena the address's pool's index claims it lies in.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000079
80 It doesn't matter whether the memory pymalloc reads up ("the
81 index") is initialized. If it's not initialized, then
82 whatever trash gets read up will lead pymalloc to conclude
Tim Petersb8b20e22004-07-07 02:46:03 +000083 (correctly) that the address isn't controlled by it, either
84 because the index is out of bounds, or the index is in bounds
85 but the arena it represents doesn't contain the address.
Neal Norwitzc3cd9df2004-06-06 19:58:40 +000086
87 This determination has to be made on every call to one of
88 pymalloc's free/realloc entry points, so its speed is critical
89 (Python allocates and frees dynamic memory at a ferocious rate
90 -- everything in Python, from integers to "stack frames",
91 lives in the heap).