I am trying to log my memory consumption per function. I am using the memory_profiler package, and I also get a final graph with my total memory consumption, but not per function as described in this nice tutorial
I am using the following script test.py
import time
from memory_profiler import profile
#-------------------------------
@profile
def test1():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
#-------------------------------
@profile
def test2():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
#-------------------------------
def main():
l1 = []
l2 = []
for i in range (5) :
print "Iteration", i
l1.append(test1())
l2.append(test2())
time.sleep(1)
if i == 2 :
del l1
l1 = []
#-------------------------------
if __name__ == "__main__":
main()
Running it via
mprof run test.py
I get some nice output like
Line # Mem usage Increment Line Contents
================================================
11 46.2 MiB 0.0 MiB @profile
12 def test2():
13 53.8 MiB 7.6 MiB a = [1] * (10 ** 6)
14 206.7 MiB 152.9 MiB b = [2] * (2 * 10 ** 7)
15 53.8 MiB -152.9 MiB del b
16 53.8 MiB 0.0 MiB return a
But the plotting it via
mprof plot
only shows the total memory consumption, and not these nice brakets, showing the execution of the different functions as described in the link. Does anyone know how to enable this?
