DEV Community

siliang4dev
siliang4dev

Posted on

[Python] Analyze Memory with `memory_profiler`

Install memory_profiler with pip,

pip install memory_profiler
Enter fullscreen mode Exit fullscreen mode

Create a test.py,

from memory_profiler import profile

@profile
def my_func():
    a = 2 ** 10
    b = 5 ** 20
    return a, b

if __name__ == "__main__":
    my_func()
Enter fullscreen mode Exit fullscreen mode

and then use memory_profiler to analyze memory,

python3 -m memory_profiler test.py
Enter fullscreen mode Exit fullscreen mode

The result will be like,

Image description

Top comments (0)