Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Python memory profiler is recommended? [closed]

I want to know the memory usage of my Python application and specifically want to know what code blocks/portions or objects are consuming most memory. Google search shows a commercial one is Python Memory Validator (Windows only).

And open source ones are PySizer and Heapy.

I haven't tried anyone, so I wanted to know which one is the best considering:

  1. Gives most details.

  2. I have to do least or no changes to my code.

like image 735
Anurag Uniyal Avatar asked Sep 21 '08 04:09

Anurag Uniyal


People also ask

What is memory profiler in Python?

This is a python module for monitoring memory consumption of a process as well as line-by-line analysis of memory consumption for python programs. It is a pure python module which depends on the psutil module.


1 Answers

My module memory_profiler is capable of printing a line-by-line report of memory usage and works on Unix and Windows (needs psutil on this last one). Output is not very detailed but the goal is to give you an overview of where the code is consuming more memory, not an exhaustive analysis on allocated objects.

After decorating your function with @profile and running your code with the -m memory_profiler flag it will print a line-by-line report like this:

Line #    Mem usage  Increment   Line Contents ==============================================      3                           @profile      4      5.97 MB    0.00 MB   def my_func():      5     13.61 MB    7.64 MB       a = [1] * (10 ** 6)      6    166.20 MB  152.59 MB       b = [2] * (2 * 10 ** 7)      7     13.61 MB -152.59 MB       del b      8     13.61 MB    0.00 MB       return a 
like image 137
Fabian Pedregosa Avatar answered Oct 13 '22 00:10

Fabian Pedregosa