A pure Python module and mprof CLI that report per-line and time-based memory use for Python programs, built on psutil.
Overview
Memory Profiler monitors memory consumption of a Python process and can break usage down line by line within functions. It installs via pip or conda-forge and depends on the psutil module for process metrics. The package is no longer actively maintained.
Line-by-line mode decorates functions with @profile and runs the script with python -m memory_profiler, printing memory after each executed line. A decorator import from memory_profiler.profile works without the -m flag, with optional precision control unless overridden by the module runner.
The mprof utility records memory over time for arbitrary executables or Python scripts, stores timestamped profiles, and plots them with matplotlib, including optional flame-style views when function entry and exit are tracked. It supports multiprocess and forked child reporting via flags such as --include-children and --multiprocess.
A programmatic memory_usage API samples RSS for a PID or while running a callable. IPython integrations provide %mprun, %%mprun, %memit, and %%memit magics when the extension is loaded. Multiple memory tracking backends include psutil, posix, and tracemalloc variants.
Key features
- Line-by-line memory usage reports
- @profile decorator and -m runner
- mprof time-series capture and plots
- Multiprocess child memory tracking
- memory_usage programmatic API
- IPython %mprun and %memit magics
- Log and logger redirect options
- Selectable memory backends
Use cases
- Finding which lines allocate memory
- Plotting memory over a run
- Checking multiprocessing memory use
- Notebook peak memory estimates
- Detecting gradual memory growth