# Try to determine how much RAM is currently being used per program.
# All interpreted programs where the interpreter is started
# by the shell or with env, will be merged to the interpreter
# (as that's what's given to exec). For e.g. all python programs
# starting with "#!/usr/bin/env python" will be grouped under python.
# You can change this by changing comm= to args= below but that will
# have the undesirable affect of splitting up programs started with
# differing parameters (for e.g. mingetty tty[1-6]).
# For older 2.6 kernels and later 2.4 redhat kernels (rmap vm without smaps)
# it can not be accurately determined how many pages are shared
# between processes in general or within a program in our case:
# A warning is printed if overestimation is possible.
# The shared calculation below will factor out shared text and
# libs etc. within a program, but not between programs. So there
# will always be some overestimation. This will be the same for
# all processes that just use libc for e.g. but more for others
# that use larger shared libs like gnome, kde etc.
# I don't take account of memory allocated for a program
# by other programs. For e.g. memory used in the X server for
# a program could be determined, but is not.
# This script assumes threads are already merged by ps
import sys, os, string
if kernel_ver == "2.4":
if open("/proc/meminfo").read().find("Inact_") == -1:
elif kernel_ver == "2.6":
for line in open("/proc/"+str(pid)+"/smaps").readlines()
return sum([int(line.split()) for line in shared_lines])
for line in os.popen("ps -e -o rss=,pid=,comm=").readlines():
size, pid, cmd = map(string.strip,line.strip().split(None,2))
if int(pid) == our_pid:
continue #no point counting this process
continue #ps gone away
if shareds[cmd] < shared:
#Note shared is always a subset of rss (trs is not always)
#Add max shared mem for each program
for cmd in cmds.keys():
sort_list = cmds.items()
sort_list=filter(lambda x:x,sort_list) #get rid of zero sized processes (kernel threads)
#The following matches "du -h" output
def human(num, power="K"):
while num >= 1000:
num /= 1024.0
return "%.1f%s" % (num,power)
for cmd in sort_list:
print "%6s %s" % (human(cmd), cmd)
#if not shared_val_accurate():
# sys.stderr.write("\nWarning: Shared memory is not reported correctly by this system.\n")
# sys.stderr.write("Values reported could be too large.\n")