Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking *maximum* memory usage by a Python function

I want to find out what the maximum amount of RAM allocated during the call to a function is (in Python). There are other questions on SO related to tracking RAM usage:

Which Python memory profiler is recommended?

How do I profile memory usage in Python?

but those seem to allow you more to track memory usage at the time the heap() method (in the case of guppy) is called. However, what I want to track is a function in an external library which I can't modify, and which grows to use a lot of RAM but then frees it once the execution of the function is complete. Is there any way to find out what the total amount of RAM used during the function call was?

like image 977
astrofrog Avatar asked Mar 24 '12 10:03

astrofrog


People also ask

How does Python track memory usage?

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 keep track of memory usage?

To open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories. However, it doesn't offer much else.

How do you run a memory profiling in Python?

Memory ProfilerThe line-by-line memory usage mode works in the same way as the line_profiler. It decorates the function you would like to profile using @profile function. You can run the script with a special script. For example, use specific arguments to the Python interpreter.

How do I limit RAM usage in Python?

Limiting CPU and Memory Usagegetrlimit() method. It returns the current soft and hard limit of the resource. Each resource is controlled by a pair of limits: a soft limit and a hard limit. The soft limit is the current limit, and may be lowered or raised by a process over time.


1 Answers

It is possible to do this with memory_profiler. The function memory_usage returns a list of values, these represent the memory usage over time (by default over chunks of .1 second). If you need the maximum, just take the max of that list. Little example:

from memory_profiler import memory_usage from time import sleep  def f():     # a function that with growing     # memory consumption     a = [0] * 1000     sleep(.1)     b = a * 100     sleep(.1)     c = b * 100     return a  mem_usage = memory_usage(f) print('Memory usage (in chunks of .1 seconds): %s' % mem_usage) print('Maximum memory usage: %s' % max(mem_usage)) 

In my case (memory_profiler 0.25) if prints the following output:

Memory usage (in chunks of .1 seconds): [45.65625, 45.734375, 46.41015625, 53.734375] Maximum memory usage: 53.734375 
like image 197
Fabian Pedregosa Avatar answered Oct 02 '22 15:10

Fabian Pedregosa