1.4. Memory Optimization#

1.4.1. Identify your bottleneck regarding memory with memory_profiler#

Want to identify which lines use the most amount of memory in your Python script?

Try memory_profiler.

memory_profiler is a Python module to make a line-by-line analysis of memory consumption for Python functions.

Below you can see how to use memory_profiler within your Python script.

  • Decorate the function you want to profile with @profile.

  • Run the script by passing the option -m memory_profiler to load the memory_profiler module.

from memory_profiler import profile

@profile
def my_func():
    a = [1] * (10 ** 6)
    b = [2] * (2 * 10 ** 7)
    del b
    return a

my_func()
!pip install memory_profiler
!python -m memory_profiler memory.py
'''
Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
     3     41.9 MiB     41.9 MiB           1   @profile
     4                                         def my_func():
     5     49.5 MiB      7.6 MiB         102       a = [i**2 for i in range(1,100)] * (10**4)
     6    194.5 MiB    145.0 MiB          22       b = [i**i for i in range(1,20)] * (10**6)
     7    194.5 MiB      0.0 MiB           1       return a, b
'''