Python 3.4+tracemallocterm
tracemalloc.Trace
traces and frames
One trace — one live memory block: its size, its domain, and the traceback of where it was allocated.
Syntax
for trace in snapshot.traces: trace.size, trace.domain, trace.traceback
Snapshot.traces is an ungrouped sequence of Trace: a program has tens of thousands of blocks, and walking them by hand is slow and awkward, which is why statistics() is used instead. You never construct a Trace yourself — the snapshot hands it out. domain is almost always 0 (Python memory); other values come from C extensions.
Examples
import tracemalloc
tracemalloc.start()
blob = bytes(50_000)
snap = tracemalloc.take_snapshot()
big = max(snap.traces, key=lambda t: t.size)
print(type(big).__name__, big.domain, big.size >= 50_000) # → Trace 0 True
print(big.traceback[0].lineno) # → 3
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy