Question: 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?](https://stackoverflow.com/questions/110259/python-memory-profiler)

[How do I profile memory usage in Python?](https://stackoverflow.com/questions/552744/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?

Answer: 
It is possible to do this with [memory\_profiler](https://pypi.python.org/pypi/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

```
Have been struggling with this task as well. After experimenting with psutil and methods from Adam, I wrote a function (credits to Adam Lewis) to measure the memory used by a specific function. People may find it easier to grab and use. 

1) [measure\_memory\_usage](https://gist.github.com/ds7711/bd2f817090eca8f66b9b23f4805f7cd6) 

2) [test measure\_memory\_usage](https://gist.github.com/ds7711/85261bf1f484cb79e65b3fb27f153d91) 

I found that materials about threading and overriding superclass are really helpful in understanding what Adam is doing in his scripts. Sorry I cannot post the links due to my "2 links" maximum limitation.