Memory Profiler

0.61.0 · maintenance · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import time
from memory_profiler import profile

# Save this as 'my_script.py'

@profile
def create_large_lists():
    a = [0] * (10 ** 6)  # Allocates ~8MB
    time.sleep(0.1)
    b = [1] * (2 * 10 ** 7) # Allocates ~160MB
    time.sleep(0.1)
    c = [2] * (5 * 10 ** 6)  # Allocates ~40MB
    del b # Frees ~160MB
    return a, c

if __name__ == '__main__':
    print("Starting memory intensive task...")
    lists = create_large_lists()
    print("Task completed.")
    # The profiling output will be printed to stdout when run via python -m memory_profiler

view raw JSON →