{"id":7432,"library":"mprofile","title":"mprofile: A Low-Overhead Memory Profiler","description":"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.","status":"active","version":"0.0.15","language":"en","source_language":"en","source_url":"https://github.com/timpalpant/mprofile","tags":["profiling","memory","performance","tracemalloc"],"install":[{"cmd":"pip install mprofile","lang":"bash","label":"Install mprofile via pip"}],"dependencies":[],"imports":[{"symbol":"mprofile","correct":"import mprofile"}],"quickstart":{"code":"import mprofile\nimport os\n\n# Start profiling with a recommended sample rate (128KB) for low overhead\nmprofile.start(sample_rate=128 * 1024)\n\n# Simulate some memory allocation\ndata = []\nfor i in range(100_000):\n    data.append(os.urandom(100)) # Allocate 100 bytes per iteration\n\n# Take a snapshot of memory usage\nsnapshot = mprofile.take_snapshot()\n\n# Print top 5 memory consuming lines and their statistics\nprint(\"Top 5 memory consuming lines:\")\nfor stat in snapshot.statistics(key_type='lineno')[:5]:\n    print(stat)\n\n# Stop profiling\nmprofile.stop()","lang":"python","description":"This quickstart demonstrates how to initialize `mprofile`, simulate some memory allocation, take a snapshot of the current memory usage, and print the top memory-consuming lines. The `sample_rate` is set to the recommended 128KB for a balance between overhead and precision."},"warnings":[{"fix":"Use Python 3.4 or newer, or follow the complex manual build process for older Python versions.","message":"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.","severity":"gotcha","affected_versions":"< 3.4"},{"fix":"Understand that mprofile provides sampled estimates, which are generally sufficient for identifying memory hotspots but may not be suitable for precise memory accounting.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to mprofile version 0.0.13 or newer (`pip install --upgrade mprofile`).","message":"Versions of mprofile prior to 0.0.13 caused a `DeprecationWarning` in Python 3.8+ due to changes in the `collections.abc` module.","severity":"deprecated","affected_versions":"< 0.0.13 (on Python 3.8+)"},{"fix":"Upgrade to mprofile version 0.0.12 or newer (`pip install --upgrade mprofile`).","message":"Versions of mprofile prior to 0.0.12 were known to cause segmentation faults when used on Ubuntu 20.04.","severity":"gotcha","affected_versions":"< 0.0.12 (on Ubuntu 20.04)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Upgrade mprofile to version 0.0.13 or newer: `pip install --upgrade mprofile`.","cause":"Running mprofile on Python 3.8+ with a version of mprofile older than 0.0.13.","error":"DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated"},{"fix":"Upgrade mprofile to version 0.0.12 or newer: `pip install --upgrade mprofile`.","cause":"This error specifically occurred on Ubuntu 20.04 with mprofile versions older than 0.0.12.","error":"Segmentation fault (core dumped)"},{"fix":"Install the package using pip: `pip install mprofile`.","cause":"The mprofile package is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'mprofile'"}]}