tracemalloc — Trace memory allocations¶
The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides the following information:
- Traceback where an object was allocated
- Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks
- Compute the differences between two snapshots to detect memory leaks
To trace most memory blocks allocated by Python, the module should be started as early as possible by setting the PYTHONTRACEMALLOC environment variable to 1. The tracemalloc.start() function can be called at runtime to start tracing Python memory allocations.
By default, a trace of an allocated memory block only stores the most recent frame (1 frame). To store 25 frames at startup: set the PYTHONTRACEMALLOC environment variable to 25.
- Project homepage (this documentation)
- Entry in the Python Cheeseshop (PyPI)
- Source code at Github
- Statistics on the project at Ohloh
- Qt graphical interface: tracemallocqt
The tracemalloc module has been integrated in Python 3.4: read tracemalloc module documentation.
Table of Contents¶
- tracemallocqt: GUI to analyze snapshots
Status of the module¶
pytracemalloc 1.0 contains patches for Python 2.7 and 3.3. The version 1.0 has been tested on Linux with Python 2.7 and 3.3: unit tests passed.
- Meliae: Python Memory Usage Analyzer
- Guppy-PE: umbrella package combining Heapy and GSL
- PySizer: developed for Python 2.4
- memprof: based on sys.getsizeof() and sys.settrace()
- Dozer: WSGI Middleware version of the CherryPy memory leak debugger