0

I am using the memory_profiler for python as follows:

from memory_profiler import profile

@profile
def my_func():
   ...

And run it normally.

Output looks like this (taken from docs):

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

Though I'd like it to be shortened to function level and look akin to this:

Line #    Mem usage  Increment    Max         Line Contents
==============================================================
     4      13.61 MB    7.64 MB   166.20 MB     my_func()
     20     23.26 MB    9.65 MB   56.20 MB     my_second_func()

Is this possible?

Roman
  • 8,826
  • 10
  • 63
  • 103
  • http://stackoverflow.com/questions/9850995/tracking-maximum-memory-usage-by-a-python-function – Sheena Aug 09 '16 at 08:56

1 Answers1

1

You can always define a third function

@profile
def my_third_func():
    my_func()
    my_second_func()

and profile this function. This should yield the output you are looking for.

Otherwise, if you are only interested in the maximum memory consumption of the different functions, mprof provides a handy visual plot for that.

Fabian Pedregosa
  • 6,329
  • 1
  • 22
  • 20