Python 3.4+tracemallocfunction
tracemalloc.get_object_traceback()
traces and frames
Returns a Traceback showing where an object was allocated, or None if its allocation was not traced.
Syntax
tracemalloc.get_object_traceback(obj) -> Traceback | None
None is returned not only for objects created before start(): CPython takes small tuples, lists and ints from its caches and free lists, and such an "allocation" never reaches the allocator. Fresh large objects (bytes(1000), class instances) are traced reliably. By default a trace holds one frame — the creating line; callers show up only with start(nframe>1).
Examples
import tracemalloc
tracemalloc.start(5)
def make():
return bytes(1000)
obj = make()
tb = tracemalloc.get_object_traceback(obj)
print([frame.lineno for frame in tb]) # → [5, 4]
tracemalloc.stop()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy