Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python tracemalloc: distinguish retained Python allocations from a process memory claim

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Tracemalloc records traced Python memory allocations so snapshots can compare where owned allocations remain reachable.

Download Python source kit

Operation contract

The owned fixture starts tracing before creating sixty-four 1KiB bytearrays. It compares a snapshot taken with the retained list against a baseline, then clears the list and collects before measuring current traced storage again. It reports only qualitative relationships with broad thresholds, rather than claiming exact bytes are portable across interpreter builds. The same data would remain retained if another object still referenced the list contents.

Failure and ownership boundary

Traced allocations are not total resident process memory, native extension buffers or proof of a leak. A valid cache may intentionally retain objects. Python weak references: a cache does not own its values, Python slots: attribute layout without deep immutability and Python lru_cache: bound retention and include the revision in the key explain why some memory remains owned. Start tracing early enough to observe the allocations you want to investigate.

Working program

python
import gc
import tracemalloc

tracemalloc.start()
try:
    baseline = tracemalloc.take_snapshot()
    retained = [bytearray(1024) for _ in range(64)]
    current, peak = tracemalloc.get_traced_memory()
    snapshot = tracemalloc.take_snapshot()
    positive_growth = sum(max(0, row.size_diff) for row in snapshot.compare_to(baseline, "lineno"))
    retained.clear()
    del retained, snapshot, baseline
    gc.collect()
    released, _ = tracemalloc.get_traced_memory()
    print("owned growth observed:", positive_growth >= 64 * 1024)
    print("release reduced current:", released < current // 2)
    print("peak includes retained:", peak >= current)
finally:
    tracemalloc.stop()

Output

Output
owned growth observed: True
release reduced current: True
peak includes retained: True

Costs and limits

Tracing adds allocation bookkeeping; storing snapshots and comparing them adds time and memory. Use a controlled workload and repeat measurements rather than converting one small fixture into a production memory ranking. Clearing references can reduce traced current storage without returning the same amount of resident memory to the operating system.

Common Mistakes

  • A high-water peak does not fall when retained objects are released.
  • Tracemalloc is not an RSS or native-buffer accounting tool.

Connected lessons

Python weak references: a cache does not own its values, Python slots: attribute layout without deep immutability, Python lru_cache: bound retention and include the revision in the key.

python
tracemalloc-retention
Storage details