Calculate memory in python


Good, I need some way to calculate the amount of memory that has been necessary to execute a series of instructions in Python. Something similar to time.clock() , but for memory, some help.

asked by Carlos Enrique Ramírez Martín 11.07.2016 в 04:35

1 answer


If you want something very detailed you could use memory_profiler but if the program is complex it may take a lot of time in giving you answers. This is used, mainly, to search memory leaks .

If you are only interested in obtaining the peak of memory used, the same creator of memory_profiler has this entry in your blog where you get this solution to get the memory used by the process that runs the Python script on any system operative:

def memory_usage_psutil():
    # return the memory usage in MB
    import psutil
    import os
    process = psutil.Process(os.getpid())
    mem = process.memory_info().rss / float(2 ** 20)
    return mem
answered by 11.07.2016 в 08:28