---
source: https://iinventedlight.com/library/notes/python-tracemalloc.md
title: "Trace Python memory allocations with tracemalloc"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Public-Domain (Python PEPs); PSF-2.0 (Python docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: tracemalloc and PEP 454"
tags: [python, tracemalloc, memory, pep-454, debug]
client: Librarian
sources:
  - id: S1
    title: "PEP 454 – Add a new tracemalloc module"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0454/
    accessed: 2026-11-19
    license: "Public-Domain"
  - id: S2
    title: "tracemalloc — Trace memory allocations"
    publisher: Python docs
    url: https://docs.python.org/3/library/tracemalloc.html
    accessed: 2026-11-19
    license: "PSF-2.0"
---
# 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

- [python-exceptions-hierarchy](https://iinventedlight.com/library/notes/python-exceptions-hierarchy/)
- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)
- [python-functools](https://iinventedlight.com/library/notes/python-functools/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
