Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand python memory profiler

I am using Memory Profiler module to get the memory usage of my python code following this answer. However, I am not able to interpret the output from %memit magic (or the output using the @profile decorator from the module or mprof run for that matter).

For instance,

%memit range(10000) 

gives me peak memory: 97.41 MiB, increment: 0.24 MiB

while,

%memit xrange(10000)

shows peak memory: 97.46 MiB, increment: 0.00 MiB. I do understand the difference between xrange returning an xrange type as opposed to range() returning a list. I used them here just to demonstrate the two scenarios.

My question is

  1. What does peak memory and increment actually mean?
  2. What should I report as the total memory usage of a script (or a function) from this output?
like image 1000
Unni Avatar asked Jul 26 '17 05:07

Unni


People also ask

How do I use memory profiler in Python?

Working with Python Memory Profiler You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.

How do I monitor memory in Python?

All you have to do is add the decorator function and the process_memory function in your code and this will give you the memory consumed by the code and it's before and after.

What is a memory profiler?

The Memory Profiler is a component in the Android Profiler that helps you identify memory leaks and memory churn that can lead to stutter, freezes, and even app crashes. It shows a realtime graph of your app's memory use and lets you capture a heap dump, force garbage collections, and track memory allocations.

How do I use Tracemalloc in Python?

To trace most memory blocks allocated by Python, the module should be started as early as possible by setting the PYTHONTRACEMALLOC environment variable to 1 , or by using -X tracemalloc command line option. The tracemalloc. start() function can be called at runtime to start tracing Python memory allocations.


1 Answers

Peak memory refers to the peak memory usage of your system (including memory usage of other processes) during the program runtime.

Increment is the increment in memory usage relative to the memory usage just before the program is run (i.e. increment = peak memory - starting memory).

So you'd report increment. Peak memory just helps you figure how close you are to using all your RAM during a program run.

If you refer to the line-by-line example in the usage section of the readme:

Line #    Mem usage  Increment   Line Contents
==============================================
     3                           @profile
     4      5.97 MB    0.00 MB   def my_func():
     5     13.61 MB    7.64 MB       a = [1] * (10 ** 6)
     6    166.20 MB  152.59 MB       b = [2] * (2 * 10 ** 7)
     7     13.61 MB -152.59 MB       del b
     8     13.61 MB    0.00 MB       return a

%memit is essentially giving you the memory usage from line 6, but reports the increment relative to line 4 (actually line 1, but presumably, there's no computation in lines 1-3).

like image 191
Ken Wei Avatar answered Oct 05 '22 06:10

Ken Wei