Finding out the memory usage of a process under Linux can be a bit confusing. That is because Linux doesn’t have one number for a process’ memory usage.
It has a bunch of different figures for a process’ memory usage! The different numbers include or exclude the virtual memory or swap usage that does not count towards a process’ physical memory usage.
The number that tells you the physical memory or main memory usage is the resident set size. You can see the resident set size for a process using this command:
ps -C -O rss |
For example on CentOS you can find out the process size of the Apache processes using this command:
ps -C httpd -O rss |
The output should be like this:
PID RSS S TTY TIME COMMAND 7409 23740 S ? 00:00:00 /usr/sbin/httpd 7416 5484 S ? 00:00:00 /usr/sbin/httpd 7903 8580 S ? 00:00:30 /usr/sbin/httpd |
The RSS column tells you the amount of non-swaped physical memory the process is using in KB. At least that is the theory. Often parts of physical memory are shared between processes so the numbers don’t always add up. In fact most processes use shared libraries that are only loaded into memory once and shared among all processes that use them.
But how to find out exactly how much RAM a set of processes like Apache are using? Well the answer is that it’s complicated. I try to estimate memory usage using this script:
#!/bin/bash ps -C $1 -O rss | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =",count; print "Memory usage per process =",sum/1024/count, "MB"; print "Total memory usage =", sum/1024, "MB" ;};' |
Save it as psmem.sh and run it like this:
[admin@serve3 ~]$ psmem httpd Number of processes = 3 Memory usage per process = 9.83464 MB Total memory usage = 29.5039 MB |
Great! thank you, it’s useful!
Hi,
My result of used memory from “free -m” is 3726MB while the result of “ps -A -O rss” with your script is 682MB.
Someone known how to precise the ps info?
When using free you have to look at the second line that says -/+ buffers cache. For example:
total used free shared buffers cached
Mem: 3916 1954 1962 0 395 765
-/+ buffers/cache: 793 3123
Swap: 4094 60 4034
The amount of used memory in the above example is 793MB not 1954MB.
So make sure you are looking at the right row in free’s output.
Hello … it is quite useful.. thanks ..
Changing RSS for SIZE in script gets better result right?
Well let’s see what the man page says:
rss: resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).
size: approximate amount of swap space that would be required if the process were to dirty all writable pages and then be swapped out. This number is very rough!
So I think rss is more relevant to estimating current physical memory usage.
Thanks for that; that saves quite a bit of work for me trying to get Apache in line!
Thank you for this shell script:
#!/bin/bash
ps -C $1 -O rss | gawk ‘{ count ++; sum += $2 }; END {count –; print “Number of processes =”,count; print “Memory usage per process =”,sum/1024/count, “MB”; print “Total memory usage =”, sum/1024, “MB” ;};’
Its result on my system is:
┌─[master@master]─[~/Publik/Root]
└──╼ sh psone.sh chromium-browser
Number of processes = 14
Memory usage per process = 51.9247 MB
Total memory usage = 726.945 MB
┌─[master@master]─[~/Publik/Root]
└──╼
This is what exactly what I am looking for. You saved my time. Jazakallahu khayran.
Super.. Use full one…
Wowzers! Thanks so much for this today, it totally helped me out this morning with helping a developer learn about a memory issue.