Memory Profiler

Introduction

In software development, understanding script performance is crucial for optimization and efficient execution. Memory Profiler is a Python library that provides detailed information about program memory usage, helping identify possible memory leaks and bottlenecks.

Good knowledge of script resource consumption gives us better control and optimization of Multivac resources.

Installation

We need to add the memory_profiler package to our virtual environment by running pip install memory_profiler.

We can use two approaches, shown below.

Example Code Alternative 1

We only need to add import memory_profiler as mp and include the lines that let us measure memory usage:

import memory_profiler as mp
def quadrat(n):
   '''
   Function that computes the square of a number.

   Args:
      n: Number for which we want to compute the square.

   Returns:
      The square of n.
   '''
   return n * n

def main():
   '''
   Main function that runs the algorithm and shows its memory profile.
   '''
   # Execute the square function with different input values
   for i in range(3):
      mp.profile(quadrat)(i)


if __name__ == "__main__":
   main()

In this case, the output will be similar to:

Filename: /home/users/alexandre.gracia/tests/memory_profiler/test.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
   3     21.5 MiB     21.5 MiB           1   def quadrat(n):
   4                                                 '''
   5                                                 Function that computes the square of a number.
   6
   7                                                 Args:
   8                                                         n: Number for which we want to compute the square.
   9
   10                                                Returns:
   11                                                        The square of n.
   12                                                '''
   13    21.5 MiB      0.0 MiB           1           return n * n


Filename: /home/users/alexandre.gracia/tests/memory_profiler/test.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
   3     21.5 MiB     21.5 MiB           1   def quadrat(n):
   4                                                 '''
   5                                                 Function that computes the square of a number.
   6
   7                                                 Args:
   8                                                         n: Number for which we want to compute the square.
   9
   10                                                Returns:
   11                                                        The square of n.
   12                                                '''
   13    21.5 MiB      0.0 MiB           1           return n * n


Filename: /home/users/alexandre.gracia/tests/memory_profiler/test.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
   3     21.5 MiB     21.5 MiB           1   def quadrat(n):
   4                                                 '''
   5                                                 Function that computes the square of a number.
   6
   7                                                 Args:
   8                                                         n: Number for which we want to compute the square.
   9
   10                                                Returns:
   11                                                        The square of n.
   12                                                '''
   13    21.5 MiB      0.0 MiB           1           return n * n

Example Code Alternative 2

In this alternative, we get a full report for the entire main function using the @mp.profile decorator.

import memory_profiler as mp

def quadrat(n):
   return n * n

@mp.profile
def main():

   for i in range(3):
      quadrat(i)

if __name__ == "__main__":
   main()

In this case, the output will be similar to:

Filename: /home/users/alexandre.gracia/tests/memory_profiler/test.py

Line #    Mem usage    Increment  Occurrences   Line Contents
=============================================================
   6     21.4 MiB     21.4 MiB           1   @mp.profile
   7                                         def main():
   8
   9     21.4 MiB      0.0 MiB           4           for i in range(3):
   10    21.4 MiB      0.0 MiB           3                   quadrat(i)