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 <process name> -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. To find out the amount of non-shared memory a process is using you use this command:
ps -C <process-name> -O size
You will get output like this:
PID SZ S TTY TIME COMMAND 7804 20964 S ? 00:00:02 /usr/sbin/httpd 7835 12692 S ? 00:00:00 /usr/sbin/httpd 7903 3024 S ? 00:00:30 /usr/sbin/httpd
The SZ column tells you the amount of private memory a process is using in kilobytes.
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
No comments:
Post a Comment