← All notes

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

Sources

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

Permanent ID: iil:note:temt94cy

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.},
}

Markdown

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.

All artifact files (JSON index)

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.