Python 3.4+tracemallocfunction
tracemalloc.start()
tracing control
Starts tracing Python memory allocations; nframe sets how many stack frames are stored for each allocation.
Syntax
tracemalloc.start(nframe=1)
Only memory allocated AFTER start() is visible: objects created earlier do not exist for tracemalloc, and get_object_traceback() returns None for them. That is why start() is called at the very top of the program, or tracing is switched on with PYTHONTRACEMALLOC=1 / -X tracemalloc. Tracing slows the program down several times and costs memory for the traces themselves; nframe=25 instead of 1 makes that noticeably worse.
Examples
import tracemalloc
early = bytes(1000)
tracemalloc.start()
late = bytes(1000)
print(tracemalloc.get_object_traceback(early)) # → None
print(tracemalloc.get_object_traceback(late) is not None) # → True
tracemalloc.stop()
import tracemalloc
tracemalloc.start(10)
print(tracemalloc.get_traceback_limit()) # → 10
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy