search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Outline
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Measuring memory usage in Python (memory_profiler)

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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
WARNING

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_profiler
nums = 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

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!