memory-profiler is a Python module designed for monitoring the memory consumption of a Python process, including detailed line-by-line analysis of memory usage within Python programs. It is built purely in Python and depends on the `psutil` module. The current version is 0.61.0. As of the latest information, the package is no longer actively maintained by its original developers.
pip install -U memory-profilerVerified import paths — ran on the pinned version, not inferred.
To perform a line-by-line memory usage analysis, decorate the function you want to profile with `@profile`. Then, run your script using the `python -m memory_profiler` command. The output will be printed to standard output, showing memory usage and increments for each line within the decorated function.
For new projects, consider exploring more actively maintained memory profiling alternatives (e.g., `memray`, `fil-profiler`). For existing projects, be aware that support might be limited.
Comment out the `from memory_profiler import profile` statement when running with `mprof run` to ensure timestamps are captured, leaving only the `@profile` decorator on functions.
Use `memory-profiler` exclusively for development and debugging. For production memory monitoring, consider less intrusive system-level tools or Python's built-in `tracemalloc` for lower overhead.
Use the `mprof run --include-children` or `--multiprocess` flags to track child processes. When using the `memory_usage` API directly, the return value will include child memory in a nested list that needs to be manually processed.
If encountering unusual behavior when profiling, test the function without the `@profile` decorator. If the issue resolves, consider using the `memory_usage` function for profiling specific code blocks, or explore non-intrusive profilers like `fil-profiler`.