search
Search
Map of Data Science
search toc
Thanks for the thanks!
close
Outline
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
near_me
Linear Algebra
52 guides
keyboard_arrow_down
1. Vectors
2. Matrices
3. Linear equations
4. Matrix determinant
5. Vector space
6. Special matrices
7. Eigenvalues and Eigenvectors
8. Orthogonality
9. Matrix decomposition
check_circle
Mark as learned
thumb_up
1
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

# Measuring memory usage in Python (memory_profiler)

schedule Mar 5, 2023
Last updated
local_offer
Python
Tags
expand_more
map
Check out the interactive map of data science

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_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:

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

Edited by 0 others
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!