Memory Profiling in Python4 min read

0
182
Profile Memory Usage in Python
5

Memory Profiling in Python Introduction

The Ultimate Goal of a software developer is to write quality code. The main aspect to think in writing quality code is looking at complexity. Complexities are divided into 2 types mainly:-

  • Time Complexity
  • Space Complexity

Time Complexity is taken care of by choosing the optimal algorithms.For Space Complexity we need to take care of memory management. For this reason, we need to know how much memory is used by the program line by line. The important application of memory profiling used by developers is it helps the developer to allocate correct memory requirements for running the server.

Here comes the exact use of the Memory Profiler Library in Python. This is a python module for monitoring memory consumption of a process as well as a line-by-line analysis of memory consumption for python programs. It is a pure python module which depends on the psutil module.

Memory Profiling in Python

Practical Implementation of Memory Profiler in Python

  • Firstly to use memory profiler we need to install it using the following command in terminal/command prompt.
pip install -U memory_profiler
  • Now for example let us consider a python code as follows:-
# A program to append the numbers ranging from 0 to 10000

@profile
def main():
  ls=[]
  for i in range(10000):
    ls.append(i)
  print("Completed")

main()
  • Here @profile must be added before function declaration to get memory profiling of that function.
  • Let’s name the above python program as example_1.py
  • Now to run this program you need to use the following command on the terminal.
python -m memory_profiler example_1.py
  • The output of the above command is as follows:-
Profile Memory Usage in Python
Memory Profiling using Python

Now if we observe the above profiling it shows the line to line increment in memory usage by the CPU. This helps the developer to take care of writing the quality code so as to reduce memory usage.

We can use function decorator in the program.This looks as follows

#The below line is known as function decorator

from memory_profiler import profile

# A program to store 2 large array's in python

@profile
def main():
  a = [1] * (10 ** 6)
  b = [2] * (2 * 10 ** 7)
  del b
  print("Completed")


main()

Here the main use of function decorator is the above python program can be run without specifying -m memory_profiler in the command line.

The Output of the program is as follows:-

Profile Memory Usage in Python
Memory Profiling for above program

Now if we observe the above profiling, In line 5, list a is created and there is an increment in the memory.In the line 6, another list b is created and there is an increment in memory usage. In line 7, the deletion of the list occurred and memory usage is reduced after deletion.

Conclusion:-

In this way we can profile memory usage line by line in python code inorder to monitor and optimize the code efficiently.

References:-

  1. https://pypi.org/project/memory-profiler/
  2. https://github.com/pythonprofilers/memory_profiler

Click 👏 on top of the article if you like this

Want to get regular updates of Free Courses, Internships & Job Opportunities and Technical Blogs to enhance your knowledge then join Dev Meet Telegram Channel or WhatsApp Group from below links

telegram channel link

Also See – Learn to Develop Image Compressor using React

Memory Profiling in Python, How do I use Memory Profiling in Python, Memory Profiling in Python, Memory Profiling in Python, How to do Memory Profiling in Python,Memory Profiling in Python,Memory Profiling in Python

LEAVE A REPLY

Please enter your comment!
Please enter your name here