The tracemalloc module traces memory blocks that Python allocates, so you can see which lines of code use the most memory and how usage changes between two points in a program. It is part of the standard library since Python 3.4, and it is the first tool to reach for when a Python process keeps growing in memory.
Tracing adds overhead, so you normally enable it in development or in a staging run, not in production by default.
tracemalloc does not measureCall tracemalloc.start() as early as possible, because only allocations made after the call are tracked. get_traced_memory() returns the current and peak sizes in bytes.
You can also start tracing for the whole process with python -X tracemalloc script.py or by setting the PYTHONTRACEMALLOC environment variable. Both accept a number that sets how many stack frames are stored per allocation.
A snapshot is a copy of the allocation records at one moment. snapshot.statistics("lineno") groups them by source line and sorts by size.
The exact figures differ by Python version and platform. Each result shows the file and line, the total size, the number of blocks and the average block size. Use "filename" to group by file, or "traceback" to group by full call stack, which needs more than one stored frame.
A memory leak shows up as growth between two snapshots taken at different times. compare_to() reports the difference per line.
The top line points at the registry.append(...) statement, with a size difference of about 10 MB. Typical causes are module-level caches, lists that are never cleared, and objects kept alive by reference cycles or callbacks. Take the first snapshot after a warm-up, so one-time allocations do not look like leaks.
Snapshots include the import system and the standard library. Filter them out to focus on your code.
Storing more frames per allocation gives better tracebacks and costs more memory and time. Tracing slows a program down and uses extra memory for its records, so measure on realistic inputs and keep the frame count small unless you need deeper stacks.
tracemalloc does not measuremalloc, is not traced.ps when the numbers disagree.start() are invisible.| Tool | Use it for |
|---|---|
tracemalloc | Lines and call stacks that allocate memory |
sys.getsizeof() | The shallow size of one object |
gc module | Finding reference cycles and uncollected objects |
memory_profiler, Memray (third party) | Line-by-line or native-level profiling |
tracemalloc slow my program down?Yes. Every allocation is recorded, so expect noticeable overhead. Use it while debugging and turn it off afterwards.
Call tracemalloc.is_tracing(). It returns True after start() and False after stop().
You can start tracing at launch and take snapshots on a signal or admin endpoint, then compare them. Accept the overhead, or do this on a single instance.
tracemalloc and sys.getsizeof()?getsizeof() returns the size of one object only, without the objects it points to. tracemalloc tracks all allocations and tells you where in the code they happened.
Start tracemalloc early, take snapshots around the suspicious work, and compare them to see which lines grow. Filter out library noise, remember that it adds overhead and covers only Python-level allocations, and combine it with gc and OS tools when you chase a stubborn leak.