0

I would like to know if it was possible to have a memory profiler that would analyse the memory taken by my python script and tell me the memory usage over time.

The goal of this would be to plot this and draw conclusions.

As usual in python, the simpler the better.

Thank you.

AdrienNK
  • 850
  • 8
  • 19

2 Answers2

2

See: https://pypi.python.org/pypi/memory_profiler

Just decorate each function you want with @profile.

Shamelessly taken from the above URL:

example.py

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

if __name__ == '__main__':
    my_func()

Then:

$ pip install memory_profiler
$ python -m memory_profiler example.py
blaze
  • 2,588
  • 3
  • 32
  • 47
  • I already use a mem-profiler but none of those that I found can get me the information I want (I insist on the over time). The end goal of what I'm doing currently would be to control the allocation of memory of the machine I'm working with so that it would allow more memory when needed. – AdrienNK Apr 27 '14 at 14:53
0

you could use any profiler like mem-profile in the other answer or even psutil for that matter and wrap around a logic to collect timestamps along with current memory usage and later use it to plot. Just stating the obvious in case you missed, HTH. Check this answer psutil for memory on how to query current memory usage using psutil

Community
  • 1
  • 1
bbsanem
  • 51
  • 10