Trace Python memory allocations with tracemalloc
Answer: The tracemalloc module is a debug tool to trace memory blocks allocated by Python. PEP 454 proposes to add a new tracemalloc module. Classic generic tools like Valgrind can get the C traceback for an allocated memory block. That does not help much for Python allocations.
What it reports
It provides a traceback where an object was allocated. It also reports statistics on allocated memory blocks per filename and per line number. Use that when you need Python-level allocation traces rather than a C allocator log.
Starting a trace
Call tracemalloc.start() before the code you want to measure. The library page documents filters, snapshots, and statistics.
| tracemalloc | report | |---|---| | tracemalloc | Debug tool to trace memory blocks allocated by Python | | traceback | Location where an object was allocated | | statistics | Per filename and per line number | | Valgrind | Classic tool that shows a C traceback instead |
See also
Claims
- id: C1 claim: PEP 454 added tracemalloc to trace memory blocks allocated by Python. source: S1 evidence: "add a new tracemalloc module to trace memory blocks allocated by Python"
- id: C2 claim: The module is a debug tool for those allocations. source: S2 evidence: "The tracemalloc module is a debug tool to trace memory blocks allocated by Python"
- id: C3 claim: It can show a traceback where an object was allocated, and per-line statistics. source: S2 evidence: "Traceback where an object was allocated"
Sources
- [S1] PEP 454 – Add a new tracemalloc module. Python PEPs. https://peps.python.org/pep-0454/ . License: Public-Domain. Accessed 2026-11-19.
- [S2] tracemalloc — Trace memory allocations. Python docs. https://docs.python.org/3/library/tracemalloc.html . License: PSF-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 454 – Add a new tracemalloc module; Python docs, tracemalloc — Trace memory allocations. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 454 – Add a new tracemalloc module; Python docs, tracemalloc — Trace memory allocations. Public-Domain; PSF-2.0.
Cite this
iinventedlight. 2026. "Trace Python memory allocations with tracemalloc." iinventedlight Library, iil:note:temt94cy, version of 2026-11-19. https://iinventedlight.com/library/notes/python-tracemalloc/. License: CC-BY-4.0.
@misc{iil_note_temt94cy,
author = {{iinventedlight}},
title = {{Trace Python memory allocations with tracemalloc}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/python-tracemalloc/},
note = {Permanent ID iil:note:temt94cy. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-tracemalloc.table.json (table; CC-BY-4.0)
- python-tracemalloc.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-tracemalloc.md.