On this page
tracemalloc
Prev: traceback | Up: stdlib_module | Next: tty
Rel: typing, annotationlib, pydoc, pydoc_data, doctest
Ref: docs.python.org — tracemalloc
Tags: #public #python3 #stdlib
Import
import tracemalloc
Overview
Built-in Python heap memory profiler (start, stop, take_snapshot, get_traced_memory) that records the exact file and line number where Python memory blocks were allocated. Reach for tracemalloc to pinpoint memory leaks, compare before/after heap snapshots, and verify peak memory budgets in tests.
Quick Start
import tracemalloc
tracemalloc.start()
data = [i for i in range(10_000)]
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
assert peak >= current > 0
Implementation Walkthrough
1. Comparing Two Heap Snapshots to Spot a Memory Leak
import tracemalloc
tracemalloc.start()
snap1 = tracemalloc.take_snapshot()
leaked = ["x" * 1000 for _ in range(500)]
snap2 = tracemalloc.take_snapshot()
top_diffs = snap2.compare_to(snap1, "lineno")
assert len(top_diffs) >= 1 and top_diffs[0].size_diff > 0
tracemalloc.stop()
2. Multi-Frame Call Stacks (tracemalloc.start(10))
Pass nframe > 1 to start(nframe) when you need the full call chain (not just the leaf allocation line) via snapshot.statistics("traceback").
Gotchas & Boundaries
- Tracks Python allocator (
PyMem) memory only: Allocations made directly by C extensions via rawmalloc()are invisible totracemalloc. - Adds CPU and memory overhead while active: Enable it during profiling, tests, or targeted diagnostics—not permanently in hot production paths.
- Always call
tracemalloc.stop()(orclear_traces()) when finished so trace tables don't keep growing.
Common Functions
- tracemalloc.clear_traces() —
clear_traces()— Clear traces of memory blocks allocated by Python. - tracemalloc.get_object_traceback() —
get_object_traceback(obj)— Get the traceback where the Python object obj was allocated. - tracemalloc.get_traceback_limit() —
get_traceback_limit()— Get the maximum number of frames stored in the traceback of a trace. - tracemalloc.get_traced_memory() —
get_traced_memory()— Get the current size and peak size of memory blocks traced by tracemalloc. - tracemalloc.get_tracemalloc_memory() —
get_tracemalloc_memory()— Get the memory usage in bytes of the tracemalloc module. - tracemalloc.is_tracing() —
is_tracing()— Return True if the tracemalloc module is tracing Python memory allocations. - tracemalloc.reset_peak() —
reset_peak()— Set the peak size of memory blocks traced by tracemalloc to the current size. - tracemalloc.start() —
start(nframe=1, /)— Start tracing Python memory allocations. - tracemalloc.stop() —
stop()— Stop tracing Python memory allocations. - tracemalloc.take_snapshot() —
take_snapshot()— Take a snapshot of traces of memory blocks allocated by Python. - tracemalloc.total_ordering() —
total_ordering(cls)— Class decorator that fills in missing ordering methods
Classes & Objects
- tracemalloc.BaseFilter —
tracemalloc.BaseFilterclass. - tracemalloc.DomainFilter —
tracemalloc.DomainFilterclass. (Methods:.domain()) - tracemalloc.Filter —
tracemalloc.Filterclass. (Methods:.filename_pattern()) - tracemalloc.Frame — Frame of a traceback. (Methods:
.filename(),.lineno()) - tracemalloc.Iterable —
tracemalloc.Iterableclass. - tracemalloc.Sequence — All the operations on a read-only sequence. (Methods:
.count(),.index()) - tracemalloc.Snapshot — Snapshot of traces of memory blocks allocated by Python. (Methods:
.compare_to(),.dump(),.filter_traces(),.load(),.statistics()) - tracemalloc.Statistic — Statistic difference on memory allocations between two Snapshot instance.
- tracemalloc.StatisticDiff — Statistic difference on memory allocations between an old and a new Snapshot instance.
- tracemalloc.Trace — Trace of a memory block. (Methods:
.domain(),.size(),.traceback()) - tracemalloc.Traceback — Sequence of Frame instances sorted from the oldest frame to the most recent frame. (Methods:
.count(),.format(),.index(),.total_nframe())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cProfile
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/sys/getallocatedblocks
- tuts/python3/stdlib_module/traceback
- tuts/python3/stdlib_module/tracemalloc/BaseFilter
- tuts/python3/stdlib_module/tracemalloc/clear_traces
- tuts/python3/stdlib_module/tracemalloc/DomainFilter
- tuts/python3/stdlib_module/tracemalloc/Filter
- tuts/python3/stdlib_module/tracemalloc/Frame
- tuts/python3/stdlib_module/tracemalloc/get_object_traceback
- tuts/python3/stdlib_module/tracemalloc/get_traceback_limit
- tuts/python3/stdlib_module/tracemalloc/get_traced_memory
- tuts/python3/stdlib_module/tracemalloc/get_tracemalloc_memory
- tuts/python3/stdlib_module/tracemalloc/is_tracing
- tuts/python3/stdlib_module/tracemalloc/Iterable
- tuts/python3/stdlib_module/tracemalloc/reset_peak
- tuts/python3/stdlib_module/tracemalloc/Sequence
- tuts/python3/stdlib_module/tracemalloc/Snapshot
- tuts/python3/stdlib_module/tracemalloc/start
- tuts/python3/stdlib_module/tracemalloc/Statistic
- tuts/python3/stdlib_module/tracemalloc/StatisticDiff
- tuts/python3/stdlib_module/tracemalloc/stop
- tuts/python3/stdlib_module/tracemalloc/take_snapshot
- tuts/python3/stdlib_module/tracemalloc/total_ordering
- tuts/python3/stdlib_module/tracemalloc/Trace
- tuts/python3/stdlib_module/tracemalloc/Traceback
- tuts/python3/stdlib_module/tty
- tuts/python3/tutorials/advanced_python_internals/memory_management_and_internals
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/traceback Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/BaseFilter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/clear_traces Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/DomainFilter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/Filter Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/Frame Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/get_object_traceback Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/get_traceback_limit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tracemalloc/get_traced_memory Linked here; links to this page; shared tags: #stdlib