Python 3.4+tracemallocterm
tracemalloc.StatisticDiff
snapshots
One row of a two-snapshot difference: size_diff and count_diff show how much memory grew or shrank at an allocation site.
Syntax
for diff in new.compare_to(old, 'lineno'): diff.size_diff, diff.count_diff
It comes from new.compare_to(old, ...) — the order matters: the snapshot you compare against is the argument. The list is sorted by the absolute value of size_diff, so the top row may be a place where memory SHRANK — check the sign. size and count are the values in the new snapshot, not the difference.
Examples
import tracemalloc
tracemalloc.start()
old = tracemalloc.take_snapshot()
leak = [bytes(100) for _ in range(1000)]
new = tracemalloc.take_snapshot()
top = new.compare_to(old, 'lineno')[0]
print(top.traceback[0].lineno, top.size_diff > 0, top.count_diff >= 1000) # → 4 True True
print(top.size >= top.size_diff) # → True
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy