Python 3.4+tracemallocterm
tracemalloc.Traceback
traces and frames
A sequence of Frame objects — the call stack at allocation time; format() renders lines like an error traceback.
Syntax
tb.format(limit=None, most_recent_first=False) -> list[str]
Since Python 3.7 frames go from the oldest to the most recent, not the other way round: tb[0] is the entry point and tb[-1] is the line that allocated the memory. format(most_recent_first=True) reverses the order for printing. There are at most nframe frames from start(), one by default — then tb[0] and tb[-1] are the same.
Examples
import tracemalloc
tracemalloc.start(5)
def inner():
return bytes(1000)
def outer():
return inner()
tb = tracemalloc.get_object_traceback(outer())
print([frame.lineno for frame in tb]) # → [7, 6, 4]
print(tb.format(most_recent_first=True)[1].strip()) # → return bytes(1000)
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy