Python Forum

Full Version: How to benchmark memory?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I have 3 solutions to the problem. I would like to choose the best one and present it (so make it sexy and verifiable). I use memory_profiler to see amount of memory that is in use now, but it shows me all the mem consumption line-by-line.

What I would like to have looks like that:
from SomeMemModule import CurrentBytesInUse
start_mem = CurrentBytesInUse()
# alloc mem and run algo
print ('Algo uses %d bytes of mem' % (CurrentBytesInUse() - start_mem) )
# dealloc mem
Is there any easy way available? Thank you in advance!
Hi,

I'm new to Python, but found the following in my recent research. I'm not sure if it is what you are looking for.
#Reference: https://stackoverflow.com/questions/938733/total-memory-used-by-python-process
#Reference: https://pypi.python.org/pypi/psutil

import os
import psutil

process = psutil.Process(os.getpid())
imemoryusage = process.memory_info().rss
print("Python memory usage is {:,} bytes.".format(imemoryusage))
Lewis