Using the following code, I can get the memory consumption of a give process in MiB:
def memory_usage_psutil():
# return the memory usage in MB
import psutil
process = psutil.Process(os.getpid())
mem = process.get_memory_info()[0] / float(2 ** 20)
return mem
How can I change this to return the percentage of memory consumption?
Update: I need to get the current value of %MEM
column when executing the top
command in terminal for a specific process.
Example: I need this function to return 14.2 for the process id of VirtualBox process.
print psutil.phymem_usage().percent
but I get a different number intop
for the running python process. – Lesialesion