Python tracemalloc Module
Example
Track memory allocations:
import tracemalloc
tracemalloc.start()
data = [i for i in range(1000)]
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
print(f'Current: {current} bytes, Peak: {peak} bytes')
Try it Yourself »
Definition and Usage
The tracemalloc module tracks memory allocations in Python programs.
Use it to find memory leaks, analyze memory usage patterns, or optimize memory-intensive applications.
Members
Member | Description |
---|---|
Filter | Class for filtering traces. |
Snapshot | Class representing a snapshot of memory allocations. |
Statistic | Class for statistics about memory allocations. |
get_traced_memory() | Return (current, peak) memory usage in bytes. |
is_tracing() | Return True if tracemalloc is tracing memory. |
start() | Start tracing memory allocations. |
stop() | Stop tracing memory allocations. |
take_snapshot() | Take a snapshot of traced memory. |