Measuring memory usage in Python (memory_profiler)
Start your free 7-days trial now!
We can use the memory_profiler
package in Python to perform a line-by-line analysis of memory usage of a function call.
If you haven't installed the package you will first need to do so:
pip install memory_profiler
Next we need to load the memory_profiler
package into our session:
%load_ext memory_profiler
Then we need to load the function we would like to measure memory usage for from a file:
from file import function
Note that the function must be imported from a file and cannot simply be defined in the session.
General syntax to get line-by-line memory usage of a function call:
%mprun -f function_name function_call(arg1, arg2, ...)
Examples
Assume we have the below function saved in a file add_dollar.py
:
def add_dollar(nums): prices = ['$'+ str(price) for price in nums] return prices
To measure the memory usage of the function call:
from add_dollar import add_dollar%load_ext memory_profilernums = range(500)
%mprun -f add_dollar add_dollar(nums)
Line # Mem usage Increment Occurences Line Contents============================================================ 1 45.3 MiB 45.3 MiB 1 def add_dollar(nums): 2 45.4 MiB 0.0 MiB 503 prices = ['$'+ str(price) for price in nums] 3 45.4 MiB 0.0 MiB 1 return prices
Interpreting the results:
Header | Explanation |
---|---|
Line # | Line number of the code |
Mem usage | Memory usage after that line has been executed |
Increment | Difference in memory between previous and current line |
Line Contents | Source code of the line that was executed |