How to measure total memory usage of all processes of a user in Python -
i use these commands see memory usage on servers:
ps -u $username -o pid,rss,command | awk '{print $0}{sum+=$2} end {print "total", sum}'
what best way collect total sum in python script? had psutil module collects global memory information , there no way filter down user.
it's pretty simple using psutil
. can iterate on processes, select ones owned , sum memory returned memory_info()
.
import psutil import getpass user = getpass.getuser() total = sum(p.memory_info()[0] p in psutil.process_iter() if p.username == user) print('total memory usage in bytes: ', total)
Comments
Post a Comment