Memory usage of a process under Linux

February 29th, 2012

Linux command line programFinding 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

8 Responses to “Memory usage of a process under Linux”

  1. tolbkni says:

    Great! thank you, it’s useful!

    Posted on 21 May 2012 Reply
  2. Silvestre says:

    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?

    Posted on 21 Jun 2012 Reply
    • Abdussamad says:

      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.

      Posted on 21 Jun 2012 Reply
  3. [...] tuning Apache MaxClients is more of an art than a science. Because of the way Linux reports process memory usage, and the different workloads Apache processes have to handle it isn’t possible to say with [...]

    Posted on 01 Aug 2012
  4. Mac says:

    Hello … it is quite useful.. thanks ..

    Posted on 28 Dec 2012 Reply
  5. Olace says:

    Changing RSS for SIZE in script gets better result right?

    Posted on 08 Apr 2013 Reply
    • Abdussamad says:

      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.

      Posted on 09 Apr 2013 Reply
  6. Tycho says:

    Thanks for that; that saves quite a bit of work for me trying to get Apache in line!

    Posted on 19 Apr 2013 Reply

Leave a Reply

Your email address will not be published. Required fields are marked *