Jupyter Snippet CB2nd 04_memprof

Jupyter Snippet CB2nd 04_memprof

4.4. Profiling the memory usage of your code with memory_profiler

%load_ext memory_profiler
%%writefile memscript.py
def my_func():
    a = [1] * 1000000
    b = [2] * 9000000
    del b
    return a
from memscript import my_func
%mprun -T mprof0 -f my_func my_func()
*** Profile printout saved to text file mprof0.
print(open('mprof0', 'r').read())
Line #  Mem usage    Increment   Line Contents
================================================
   1     93.4 MiB      0.0 MiB   def my_func():
   2    100.9 MiB      7.5 MiB       a = [1] * 1000000
   3    169.7 MiB     68.8 MiB       b = [2] * 9000000
   4    101.1 MiB    -68.6 MiB       del b
   5    101.1 MiB      0.0 MiB       return a
%%memit import numpy as np
np.random.randn(1000000)
peak memory: 101.20 MiB, increment: 7.77 MiB