tracemalloc
Stdlib — ProfilingPython 2.0+Intermediate
Trace memory allocations to find leaks
Quick Info
- Documentation
- Official Docs
- Python Version
- 2.0+
- Dependencies
- None — Python Standard Library
- Install
Included with Python
Learn by Difficulty
Quick Example
python
import tracemalloc tracemalloc.start() # Allocate some memory data = [list(range(1000)) for _ in range(100)] current, peak = tracemalloc.get_traced_memory() print(f"Current: {current / 1024:.1f} KB") print(f"Peak: {peak / 1024:.1f} KB") tracemalloc.stop()
The tracemalloc module is part of Python's standard library. Trace memory allocations to find leaks.
Try in PlaygroundTags
stdlibprofilingperformance