Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I profile memory usage in Python?

I've recently become interested in algorithms and have begun exploring them by writing a naive implementation and then optimizing it in various ways.

I'm already familiar with the standard Python module for profiling runtime (for most things I've found the timeit magic function in IPython to be sufficient), but I'm also interested in memory usage so I can explore those tradeoffs as well (e.g. the cost of caching a table of previously computed values versus recomputing them as needed). Is there a module that will profile the memory usage of a given function for me?

like image 488
Lawrence Johnston Avatar asked Feb 16 '09 09:02

Lawrence Johnston


People also ask

How do I check RAM usage in Python?

The function psutil. virutal_memory() returns a named tuple about system memory usage. The third field in tuple represents the percentage use of the memory(RAM).

How does memory profiler work Python?

Memory Profiler is a pure Python module that uses the psutil module. It monitors the memory consumption of a Python job process. Also, it performs a line-by-line analysis of the memory consumption of the application. The line-by-line memory usage mode works in the same way as the line_profiler.

What is memory usage in Python?

Mem Usage can be tracked to observe the total memory occupancy by the Python interpreter, whereas the Increment column can be observed to see the memory consumption for a particular line of code. By observing the memory usage one can optimize the memory consumption to develop a production-ready code.


1 Answers

This one has been answered already here: Python memory profiler

Basically you do something like that (cited from Guppy-PE):

>>> from guppy import hpy; h=hpy() >>> h.heap() Partition of a set of 48477 objects. Total size = 3265516 bytes.  Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)      0  25773  53  1612820  49   1612820  49 str      1  11699  24   483960  15   2096780  64 tuple      2    174   0   241584   7   2338364  72 dict of module      3   3478   7   222592   7   2560956  78 types.CodeType      4   3296   7   184576   6   2745532  84 function      5    401   1   175112   5   2920644  89 dict of class      6    108   0    81888   3   3002532  92 dict (no owner)      7    114   0    79632   2   3082164  94 dict of type      8    117   0    51336   2   3133500  96 type      9    667   1    24012   1   3157512  97 __builtin__.wrapper_descriptor <76 more rows. Type e.g. '_.more' to view.> >>> h.iso(1,[],{}) Partition of a set of 3 objects. Total size = 176 bytes.  Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)      0      1  33      136  77       136  77 dict (no owner)      1      1  33       28  16       164  93 list      2      1  33       12   7       176 100 int >>> x=[] >>> h.iso(x).sp  0: h.Root.i0_modules['__main__'].__dict__['x'] >>>  
like image 190
Hubert Avatar answered Sep 28 '22 08:09

Hubert