Profiling and Memory Modules

tracemalloc

Tracks Python memory allocations and their allocation tracebacks. Snapshots can be compared to investigate allocation growth; get_traced_memory reports current and peak traced bytes. It does not measure all native allocations or total process memory. Stop tracing when finished.

Formal Syntax & Structure

python syntaxpython
import tracemalloc

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import tracemalloctracemalloc.start()try:    data = bytearray(4096)    current, peak = tracemalloc.get_traced_memory()    print(peak >= current >= 4096)finally:    tracemalloc.stop()
Python Reference