explaingit

pythonprofilers/memory_profiler

4,569PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

A Python tool that shows exactly how much memory each line of your code uses. Add a decorator to any function, run it, and get a line-by-line table showing where memory grows or shrinks.

Mindmap

mindmap
  root((memory_profiler))
    Line-by-line Mode
      Decorator usage
      Memory per line
      Memory delta
    Time-based Mode
      mprof run
      Plot graph
      Function markers
    Multi-process
      Child tracking
      Combined totals
    Integration
      Jupyter notebooks
      pip install
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add @profile to a Python function and run memory_profiler to pinpoint which line is causing a memory spike.

USE CASE 2

Use mprof run to record total memory over the lifetime of a long-running Python script, then plot it as a graph.

USE CASE 3

Profile a multi-process Python program and track child process memory separately or combined with the parent.

USE CASE 4

Run memory profiling interactively inside a Jupyter notebook using the built-in magic command.

Tech stack

Pythonpsutilmatplotlib

Getting it running

Difficulty · easy Time to first run · 5min

Package is no longer actively maintained, it still works but may not support the latest Python versions.

In plain English

memory_profiler is a Python tool for measuring how much memory your Python code uses while it runs. It has two main modes: line-by-line analysis and time-based tracking. The README notes upfront that the package is no longer actively maintained. The line-by-line mode works by adding a @profile decorator to any Python function you want to inspect. When you run your script with a special command, the tool prints a table showing each line of that function, how much memory the Python process was using after that line ran, and how much memory that line added or freed. This makes it straightforward to spot which specific lines in your code are responsible for large memory increases. The time-based mode uses a separate command called mprof. You run mprof run followed by your script, and the tool records total memory usage over the full lifetime of the process. Afterward, mprof plot generates a graph of memory over time using matplotlib. If you also decorate functions with @profile, the graph can show exactly when each function was entered and exited, helping identify which part of the program caused a memory spike. The tool also handles programs that spawn multiple processes. You can tell mprof to track child processes separately or add their memory together with the parent's, giving a more accurate picture of total resource use in parallel workloads. Installation is a single pip install command. The package depends on psutil, a standard cross-platform library for reading system metrics. It also works inside Jupyter notebooks through a built-in magic command, so you can profile code interactively without leaving the notebook environment.

Copy-paste prompts

Prompt 1
Show me how to add the @profile decorator to my Python function and run memory_profiler to see which lines use the most memory.
Prompt 2
Write a script that uses mprof to profile my Python data pipeline and generate a memory-over-time graph with matplotlib.
Prompt 3
I'm using memory_profiler in a Jupyter notebook, give me the magic command syntax to profile a specific function.
Prompt 4
My Python script spawns multiple worker processes. How do I use mprof to track child process memory separately from the parent?
Open on GitHub → Explain another repo

← pythonprofilers on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.