RAM Physical Memory usage by each Process.


 
Thread Tools Search this Thread
Operating Systems Solaris RAM Physical Memory usage by each Process.
# 8  
Old 11-18-2007
Quote:
Originally Posted by bsrajirs
Yes, i used the Top command and when i sum up the memory usage shown (such as 'res' or 'size') is becoming more than the RAM size. So i am not sure how to calculate the RAM usage be each processes..
Unless I'm very wrong, they should sum up to the amount of virtual memory used (that includes any swap space too).
# 9  
Old 11-19-2007
Consider two different users, each running the editor vi. Each process will have its own data segment and its own stack segment. But the two processes share a common text segment. And vi uses the standard I/O library, stdio. This is a shared library. One copy serves both vi processes. And it is probably serving dozens of other processes as well. Memory is not simply allocated to a process and the question really has no simple answer.
# 10  
Old 11-19-2007
ps -eo pid,vsz,args | grep <pid>

This would give you the entire space occupied by the process

But there are two things you need to notice here.
1) Solaris memory works under Virtual Memory - which is RAM + Swap. Until and unless you really get into each and every file used by the program its really hard to find which is placed in RAM and which one in swap

2) As the above post says, some dynamic files are used by multiple processes. So if you go on adding all the files used by a process for all the processes, it will end up more than ur RAM + Swap

pmap <pid> will tell you all the files used by the process along with their addresses and space occupied. If you really want to dig inside the process, i suggest you use pmap to calculate


Thanks
~Sage
# 11  
Old 11-19-2007
A frined of mine gave me that script some time ago, I modified it a bit, but it will work only in Linux, sorry for the off-topic, I just want to post it, because it's for the same subject I believe.
Code:
#!/usr/bin/env python


# Try to determine how much RAM is currently being used per program.
# Note per _program_, not per process. So for example this script
# will report RAM used by all httpd process together. In detail it reports:
# sum(private RAM for program processes) + sum(Shared RAM for program processes)
# The shared RAM is problematic to calculate, and this script automatically
# selects the most accurate method available for your kernel.
# Notes:
#
# A warning is printed if overestimation is possible, depending on the kernel specs.
#
# 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.

import sys, os, string

if os.geteuid() != 0:
    sys.stderr.write("You must be root user to run this script!\n");
    sys.exit(1)

PAGESIZE=os.sysconf("SC_PAGE_SIZE")/1024 #KiB
our_pid=os.getpid()

#(major,minor,release)
def kernel_ver():
    kv=open("/proc/sys/kernel/osrelease").readline().split(".")[:3]
    for char in "-_":
        kv[2]=kv[2].split(char)[0]
    return (int(kv[0]), int(kv[1]), int(kv[2]))

kv=kernel_ver()

have_smaps=0
have_pss=0

#return Rss,Pss,Shared
#note Private = Rss-Shared
def getMemStats(pid):
    Shared_lines=[]
    Pss_lines=[]
    Rss=int(open("/proc/"+str(pid)+"/statm").readline().split()[1])*PAGESIZE
    if os.path.exists("/proc/"+str(pid)+"/smaps"): #stat
        global have_smaps
        have_smaps=1
        for line in open("/proc/"+str(pid)+"/smaps").readlines(): #open
            #Note in smaps Shared+Private = Rss above
            #The Rss in smaps includes video card mem etc.
            if line.startswith("Shared"):
                Shared_lines.append(line)
            elif line.startswith("Pss"):
                global have_pss
                have_pss=1
                Pss_lines.append(line)
        Shared=sum([int(line.split()[1]) for line in Shared_lines])
        Pss=sum([int(line.split()[1]) for line in Pss_lines])
    elif (2,6,1) <= kv <= (2,6,9):
        Pss=0
        Shared=0 #lots of overestimation, but what can we do?
    else:
        Pss=0
        Shared=int(open("/proc/"+str(pid)+"/statm").readline().split()[2])*PAGESIZE
    return (Rss, Pss, Shared)

cmds={}
shareds={}
count={}
for pid in os.listdir("/proc/"):
    try:
        pid = int(pid) #note Thread IDs not listed in /proc/
        if pid ==our_pid: continue
    except:
        continue
    cmd = file("/proc/%d/status" % pid).readline()[6:-1]
    try:
        exe = os.path.basename(os.path.realpath("/proc/%d/exe" % pid))
        if exe.startswith(cmd):
            cmd=exe #show non truncated version
            #Note because we show the non truncated name
            #one can have separated programs as follows:
            #584.0 KiB +   1.0 MiB =   1.6 MiB    mozilla-thunder (exe -> bash)
            # 56.0 MiB +  22.2 MiB =  78.2 MiB    mozilla-thunderbird-bin
    except:
        #permission denied or
        #kernel threads don't have exe links or
        #process gone
        continue
    try:
        rss, pss, shared = getMemStats(pid)
        private = rss-shared
        #Note shared is always a subset of rss (trs is not always)
    except:
        continue #process gone
    if shareds.get(cmd):
        if pss: #add shared portion of PSS together
            shareds[cmd]+=pss-private
        elif shareds[cmd] < shared: #just take largest shared val
            shareds[cmd]=shared
    else:
        if pss:
            shareds[cmd]=pss-private
        else:
            shareds[cmd]=shared
    cmds[cmd]=cmds.setdefault(cmd,0)+private
    if count.has_key(cmd):
       count[cmd] += 1
    else:
       count[cmd] = 1

#Add max shared mem for each program
total=0
for cmd in cmds.keys():
    cmds[cmd]=cmds[cmd]+shareds[cmd]
    total+=cmds[cmd] #valid if PSS available

sort_list = cmds.items()
sort_list.sort(lambda x,y:cmp(x[1],y[1]))
sort_list=filter(lambda x:x[1],sort_list) #get rid of zero sized processes

#The following matches "du -h" output
#see also human.py
def human(num, power="Ki"):
    powers=["Ki","Mi","Gi","Ti"]
    while num >= 1000: #4 digits
        num /= 1024.0
        power=powers[powers.index(power)+1]
    return "%.1f %s" % (num,power)

def cmd_with_count(cmd, count):
    if count>1:
       return "%s (%u)" % (cmd, count)
    else:
       return cmd

print " Private  +   Shared  =  RAM used\tProgram \n"
for cmd in sort_list:
    print "%8sB + %8sB = %8sB\t%s" % (human(cmd[1]-shareds[cmd[0]]), human(shareds[cmd[0]]), human(cmd[1]),
                                      cmd_with_count(cmd[0], count[cmd[0]]))
if have_pss:
    print "-" * 33
    print "                        %sB" % human(total)
    print "=" * 33
print "\n Private  +   Shared  =  RAM used\tProgram \n"

#Warn of possible inaccuracies
#2 = accurate & can total
#1 = accurate only considering each process in isolation
#0 = some shared mem not reported
#-1= all shared mem not reported
def shared_val_accuracy():
    """http://wiki.apache.org/spamassassin/TopSharedMemoryBug"""
    if kv[:2] == (2,4):
        if open("/proc/meminfo").read().find("Inact_") == -1:
            return 1
        return 0
    elif kv[:2] == (2,6):
        if os.path.exists("/proc/"+str(os.getpid())+"/smaps"):
            if open("/proc/"+str(os.getpid())+"/smaps").read().find("Pss:") != -1:
                return 2
            else:
                return 1
        if (2,6,1) <= kv <= (2,6,9):
            return -1
        return 0
    else:
        return 1

def current_memory_status():

	print "Displaying currently available memory...\n"
	print "--------------------------------------------------------------------------"
	showSysMemory = 'free -lmt'
	os.system(showSysMemory)
	print "--------------------------------------------------------------------------"
	print "\n"

current_memory_status()

vm_accuracy = shared_val_accuracy()
if vm_accuracy == -1:
    sys.stderr.write("Warning: Shared memory is not reported by this system.\n")
    sys.stderr.write("Values reported will be too large, and totals are not reported\n")
elif vm_accuracy == 0:
    sys.stderr.write("Warning: Shared memory is not reported accurately by this system.\n")
    sys.stderr.write("Values reported could be too large, and totals are not reported\n")
elif vm_accuracy == 1:
    sys.stderr.write("Warning: Shared memory is slightly over-estimated by this system\n")
    sys.stderr.write("for each program, so totals are not reported.\n")

# 12  
Old 11-19-2007
Sample output report :
Quote:
Private + Shared = RAM used Program

4.0 KiB + 0.0 KiB = 4.0 KiB rpc.statd
4.0 KiB + 0.0 KiB = 4.0 KiB xinetd
24.0 KiB + 36.0 KiB = 60.0 KiB gpm
28.0 KiB + 32.0 KiB = 60.0 KiB xfs
48.0 KiB + 72.0 KiB = 120.0 KiB rhnsd
52.0 KiB + 100.0 KiB = 152.0 KiB init
52.0 KiB + 116.0 KiB = 168.0 KiB klogd
44.0 KiB + 124.0 KiB = 168.0 KiB atd
44.0 KiB + 124.0 KiB = 168.0 KiB mdadm
68.0 KiB + 144.0 KiB = 212.0 KiB auditd
56.0 KiB + 184.0 KiB = 240.0 KiB irqbalance
76.0 KiB + 244.0 KiB = 320.0 KiB syslogd
76.0 KiB + 304.0 KiB = 380.0 KiB portmap
72.0 KiB + 376.0 KiB = 448.0 KiB mingetty (6)
84.0 KiB + 380.0 KiB = 464.0 KiB crond
80.0 KiB + 628.0 KiB = 708.0 KiB rpc.rquotad
132.0 KiB + 728.0 KiB = 860.0 KiB rpc.mountd
244.0 KiB + 1.1 MiB = 1.4 MiB bash
452.0 KiB + 1.5 MiB = 1.9 MiB sshd (2)

Private + Shared = RAM used Program

Displaying currently available memory...

--------------------------------------------------------------------------
total used free shared buffers cached
Mem: 3514 3484 30 0 171 2954
Low: 826 810 16 0 0 0
High: 2687 2674 13 0 0 0
-/+ buffers/cache: 358 3156
Swap: 4094 3 4091
Total: 7609 3487 4121
--------------------------------------------------------------------------
# 13  
Old 11-19-2007
Check the pmap utility.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Process memory usage

hi all gurus: I want to find out Solaris process memory usage, but got a bit confused, see if any one can provide me some guidance. i tend to use prstat -a to get total memory consumption by user (I know prstat likely have a bug that simply sum up the memory, regardless if the memory being... (5 Replies)
Discussion started by: oakville
5 Replies

2. UNIX for Dummies Questions & Answers

Average CPU and RAM usage for a process

Hi, I will be creating a process myself and I want to know the average CPU and RAM used by the process over the lifetime of the process. I see that there are various tools available(pidstat) for doing , I was wondering if it possible to do it in a single command while creation. Thanks in... (3 Replies)
Discussion started by: koustubh
3 Replies

3. UNIX for Advanced & Expert Users

Memory Usage(Physical) in one Word? Suse Linux.

Experts, I have been trying to figure out what is the total physical memory used from this output: And what is the free memory available for the application or any programs. The answer has to be in this format: 1. Physical Memory Used= xx.xx% 2. Physical Memry available= yy.yy% ... (5 Replies)
Discussion started by: rveri
5 Replies

4. HP-UX

Virtual Memory Usage a Process

Hi all, Is there any command which shows the virtual memory usage of a particular process in HP-UX machine. I have tried with ps, top but could not get what I want. Kindly provide me a solution. Thanks in Advance ARD (4 Replies)
Discussion started by: ard
4 Replies

5. UNIX for Advanced & Expert Users

collecting memory usage by a process

Hi Guys, I work on a AIX environment and I'm trying to write a script where I can collect all the memory used by a process. Basically I'm executing the command 'ps -fu userid' to get all the process ids and then executing the 'ps v PID' to get all the memory allocated by PPID. My question is... (2 Replies)
Discussion started by: arizah
2 Replies

6. AIX

AIX 5.3 Physical Memory usage

Hi, I have AIX 5.3TL8 two node cluster using HACMP and have 10g database using RAW devices. I am seeing gradual increase in comp% memory everyday and it reaches 100% and evicts the node, we had 4 evictions in 40days. I am pasting vmstat and vmo output, anyone seen this issue? ... (5 Replies)
Discussion started by: navin7386
5 Replies

7. HP-UX

how could I get a process Memory Usage

I use pstat API to get Process Infomation I would like to get a process 1.process owner 2.how many physical memory and virtual memory and total memory used(KB) and usage(%) 3.a process excution file create time 4.a process excution file access time I do't know which attribute it i need ... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Red Hat

red hat Linux 5.0 is detecting 3gb ram but physical ram is 16gb

Hi, On server 64bit Hw Arch , Linux 5.0(32bit) is installed it is showing only 3gb of ram though physical is 16gb can u give me idea why? (4 Replies)
Discussion started by: manoj.solaris
4 Replies

9. Shell Programming and Scripting

Memory usage of a process

hi all, i want to write a script that checks the memory usage of processes and send a mail with the name of the process witch is using more then 300mb RAM. dose anybody have a sample script or an idea how i can make it ? PROCCESSES="snmpd sendmail" for myVar in $PROCCESSES do ... (7 Replies)
Discussion started by: tafil
7 Replies

10. UNIX for Advanced & Expert Users

how to restrict memory usage by a process

we are running red hat ES4 and i would like to know if there is anyway of restrcting the maximum amount of memory that a process can get? I have a single preocess that is taking >13GB. Thanks, Frank (4 Replies)
Discussion started by: frankkahle
4 Replies
Login or Register to Ask a Question