on.</p><h2 id="a53a">Installation:</h2><p id="0ff0">Memory Profiler can be installed from PyPl using:</p><div id="de37"><pre>pip <span class="hljs-keyword">install</span> -U memory_profiler</pre></div><p id="d187">and can be imported using</p><div id="3752"><pre><span class="hljs-keyword">from</span> memory_profiler <span class="hljs-keyword">import</span> profile</pre></div><h2 id="6156">Usage:</h2><p id="2314">After everything is set up, it's pretty easy to use this module to track the memory consumption of the function. <code>@profile</code> decorator can be used before every function that needs to be tracked. This will track the memory consumption line-by-line in the same way as of <a href="https://pypi.org/project/line-profiler/">line-profiler</a>.</p><p id="f468">After decorating all the functions with <code>@profile</code> execute the python script with a specific set of arguments.</p>
<figure id="b96f">
<div>
<div>
<iframe class="gist-iframe" src="/gist/satkr7/0102b4e5e2acff9db15809298e77a1d1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="7b9f">Execute the above Python script using bypassing <code>-m memory profiler</code> to the Python interpreter. This will load the memory_profiler module and print the memory consumption line-by-line.</p><p id="ebf8">Use the below to execute the Python script along with the memory profiler.</p><div id="f938"><pre><span class="hljs-keyword">python</span> -<span class="hljs-keyword">m</span> memory_profiler <span class="hljs-symbol"><filename></span>.<span class="hljs-keyword">py</span></pre></div><figure id="8ba9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*--WZVx_xXuDeyE2slwvmjA.png"><figcaption>(Image by Author)</figcaption></figure><p id="6a84">After successful execution, you will get a line-by-line memory consumption report, similar to the above image. The report has 5 columns:</p><ul><li><b>Line #</b>: Line Number</li><li><b>Line Contents</b>: Python code at each line number</li><li><b>Mem usage</b>: Memory usage by the Python interpreter after every execution of the li
Options
ne.</li><li><b>Increment</b>: Difference in memory consumption from the current line to the last line. It basically denotes the memory consumed by a particular line of Python code.</li><li><b>Occurrences</b>: Number of times a particular line of code is executed.</li></ul><p id="fd15">Mem Usage can be tracked to observe the total memory occupancy by the Python interpreter, whereas the Increment column can be observed to see the memory consumption for a particular line of code. By observing the memory usage one can optimize the memory consumption to develop a production-ready code.</p><h1 id="f44f">Conclusion:</h1><p id="dbbd">Optimizing the memory consumption is as important as optimizing the time complexity of the Python code. By optimizing the memory consumption, one can speed up the execution to some extent and avoid memory crashes.</p><p id="9278">One can also try custom <code>@profile</code> decorators to specify the precision of the argument. Read the <a href="https://pypi.org/project/memory-profiler/">documentation</a> of the memory profiler module for better understanding.</p><h1 id="3c87">References:</h1><p id="e914">[1] Memory Profiler Documentation: <a href="https://pypi.org/project/memory-profiler/">https://pypi.org/project/memory-profiler/</a></p><p id="ff83"><i>Loved the article? Become a <a href="https://satyam-kumar.medium.com/membership">Medium member</a> to continue learning without limits. I’ll receive a small portion of your membership fee if you use the following link, with no extra cost to you.</i></p><div id="fbae" class="link-block">
<a href="https://satyam-kumar.medium.com/membership">
<div>
<div>
<h2>Join Medium with my referral link - Satyam Kumar</h2>
<div><h3>As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…</h3></div>
<div><p>satyam-kumar.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*sp1Stkiu2tDeRpx8)"></div>
</div>
</div>
</a>
</div><p id="e94c" type="7">Thank You for Reading</p></article></body>
Profile Memory Consumption of Python functions in a single line of code
Monitor line-by-line memory usage of functions with memory profiler module
Python is a popular language among the data science community known for its robustness and vast presence of frameworks. Python prefers simplicity over complexity which makes it more popular but compromising the performance. Python programs are often prone to memory management issues.
Data Scientists use Python language to process a vast amount of data on a fixed memory constraint. If the code execution exceeds the RAM limit, one may experience memory errors and the program execution terminates. A quick solution is to increase the memory allocation to avoid memory management issues, but this is not always feasible.
Why Memory Profiling is Important?
Often newbies do not release unused memories and continuously assign new variables, that may increase memory consumption. When the code is been executed, more and more memory is been assigned. Python automatically manages the memory, but it may fail to return the memory consumption to the operating system while executing long Python codes.
So external tools are required to monitor the memory consumption that will further help to optimize the memory.
This article will discuss the memory profiler module that monitors the memory consumption of the Python functions.
Memory Profiler:
Memory Profiler is an open-source Python module that uses psutilmodule internally, to monitor the memory consumption of Python functions. It performs a line-by-line memory consumption analysis of the function.
Installation:
Memory Profiler can be installed from PyPl using:
pip install -U memory_profiler
and can be imported using
from memory_profiler import profile
Usage:
After everything is set up, it's pretty easy to use this module to track the memory consumption of the function. @profile decorator can be used before every function that needs to be tracked. This will track the memory consumption line-by-line in the same way as of line-profiler.
After decorating all the functions with @profile execute the python script with a specific set of arguments.
Execute the above Python script using bypassing -m memory profiler to the Python interpreter. This will load the memory_profiler module and print the memory consumption line-by-line.
Use the below to execute the Python script along with the memory profiler.
python -m memory_profiler <filename>.py
(Image by Author)
After successful execution, you will get a line-by-line memory consumption report, similar to the above image. The report has 5 columns:
Line #: Line Number
Line Contents: Python code at each line number
Mem usage: Memory usage by the Python interpreter after every execution of the line.
Increment: Difference in memory consumption from the current line to the last line. It basically denotes the memory consumed by a particular line of Python code.
Occurrences: Number of times a particular line of code is executed.
Mem Usage can be tracked to observe the total memory occupancy by the Python interpreter, whereas the Increment column can be observed to see the memory consumption for a particular line of code. By observing the memory usage one can optimize the memory consumption to develop a production-ready code.
Conclusion:
Optimizing the memory consumption is as important as optimizing the time complexity of the Python code. By optimizing the memory consumption, one can speed up the execution to some extent and avoid memory crashes.
One can also try custom @profile decorators to specify the precision of the argument. Read the documentation of the memory profiler module for better understanding.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a small portion of your membership fee if you use the following link, with no extra cost to you.