5

Please excuse this naive question of mine. I am trying to monitor memory usage of my python code, and have come across the promising memory_profiler package. I have a question about interpreting the output generated by @profile decorator.

Here is a sample output that I get by running my dummy code below:

dummy.py

from memory_profiler import profile

@profile
def my_func():
   a = [1] * (10 ** 6)
   b = [2] * (2 * 10 ** 7)
   del b
   return a

if __name__ == '__main__':
   my_func()

Calling dummy.py by "python dummy.py" returns the table below.

Line # Mem usage Increment Line Contents

 3      8.2 MiB      0.0 MiB   @profile
 4                             def my_func():
 5     15.8 MiB      7.6 MiB       a = [1] * (10 ** 6)
 6    168.4 MiB    152.6 MiB       b = [2] * (2 * 10 ** 7)
 7     15.8 MiB   -152.6 MiB       del b
 8     15.8 MiB      0.0 MiB       return a

My question is what does the 8.2 MiB in the first line of the table correspond to. My guess is that it is the initial memory usage by the python interpreter itself; but I am not sure. If that is the case, is there a way to have this baseline usage automatically subtracted from the memory usage of the script?

Many thanks for your time and consideration!

Noushin

4
  • In the future, when asking about a third-party module (especially one that's not widely-known like NumPy), please provide a link. I added it for you this time. Commented Dec 11, 2013 at 21:40
  • Thanks abarnert! Will keep that in mind :) Now that you mentioned NumPy, Can I be bold and ask you another question? Do you know if memory_profiler returns accurate numbers for the case of using NumPy arrays? (I was getting fishy results by sys.getsizeof calls, before discovering the built-in "nbytes" module of arrays. Just wondering if I should expect similar problems here) Commented Dec 11, 2013 at 22:16
  • I have no idea. But presumably it's getting memory usage stats for your process from the OS or heap stats or something similar, not walking the Python allocators. If so, any memory that's being used will be counted, even if you can't figure out who's using it. So, I'd give 10:1 odds or better that, at least in a single-threaded program, you'll have nothing to worry about there. But really, you might be better off asking the author. Commented Dec 11, 2013 at 22:30
  • Looks like ChatGPT is using this example to explain the basics of memory_profile. Commented Jan 5, 2024 at 12:52

2 Answers 2

5

According to the docs:

The first column represents the line number of the code that has been profiled, the second column (Mem usage) the memory usage of the Python interpreter after that line has been executed. The third column (Increment) represents the difference in memory of the current line with respect to the last one.

So, that 8.2 MiB is the memory usage after the first line has been executed. That includes the memory needed to start up Python, load your script and all of its imports (including memory_profiler itself), and so on.

There don't appear to be any documented options for removing that from each entry. But it wouldn't be too hard to post-process the results.

Alternatively, do you really need to do that? The third column shows how much additional memory has been used after each line, and either that, or the sum of that across a range of lines, seems more interesting than the difference between each line's second column and the start.

Sign up to request clarification or add additional context in comments.

Comments

0

The difference in memory between lines is given in the second column or you could write a small script to process the output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.