mprofile: A Low-Overhead Memory Profiler
mprofile is a low-overhead sampling memory profiler for Python, derived from heapprof, with an interface similar to tracemalloc. It attempts to give results comparable to tracemalloc, but uses statistical sampling to lower memory and CPU overhead. The sampling algorithm is the one used by tcmalloc and Golang heap profilers. The current version is 0.0.15, and it is compatible with Python >= 3.4.
Common errors
-
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated
cause Running mprofile on Python 3.8+ with a version of mprofile older than 0.0.13.fixUpgrade mprofile to version 0.0.13 or newer: `pip install --upgrade mprofile`. -
Segmentation fault (core dumped)
cause This error specifically occurred on Ubuntu 20.04 with mprofile versions older than 0.0.12.fixUpgrade mprofile to version 0.0.12 or newer: `pip install --upgrade mprofile`. -
ModuleNotFoundError: No module named 'mprofile'
cause The mprofile package is not installed in the current Python environment.fixInstall the package using pip: `pip install mprofile`.
Warnings
- gotcha mprofile is compatible with Python >= 3.4. Using it with earlier versions of Python (< 3.4) requires building CPython from source and manually applying `pytracemalloc` patches, which is a complex setup.
- gotcha mprofile uses statistical sampling for memory profiling, not comprehensive tracing like `tracemalloc`. This design choice prioritizes low overhead, meaning the reported memory usage is an estimate and may not be an exact, byte-for-byte measurement. Interpret results with this in mind.
- deprecated Versions of mprofile prior to 0.0.13 caused a `DeprecationWarning` in Python 3.8+ due to changes in the `collections.abc` module.
- gotcha Versions of mprofile prior to 0.0.12 were known to cause segmentation faults when used on Ubuntu 20.04.
Install
-
pip install mprofile
Imports
- mprofile
import mprofile
Quickstart
import mprofile
import os
# Start profiling with a recommended sample rate (128KB) for low overhead
mprofile.start(sample_rate=128 * 1024)
# Simulate some memory allocation
data = []
for i in range(100_000):
data.append(os.urandom(100)) # Allocate 100 bytes per iteration
# Take a snapshot of memory usage
snapshot = mprofile.take_snapshot()
# Print top 5 memory consuming lines and their statistics
print("Top 5 memory consuming lines:")
for stat in snapshot.statistics(key_type='lineno')[:5]:
print(stat)
# Stop profiling
mprofile.stop()