Whether you are a GNU/Linux sysadmin or desktop user, you might
suspect a process of leaking memory, or be curious about just how much
memory your biggest running processes are using. Most people know
about the 'top' command, which gives a continuously updating display
of all system processes, along with memory, swap and load usage. But
sometimes you want to be able to see targeted process data, without a
cluttered display. Here is a quick way to display process memory
usage, sorted and limited to the top ten, using the 'ps' command:
Here is what the output looks like:
This will show just the command name and resident set size (rss) in KB of a subset of system processes. The resident set size is the non-swapped physical memory used by each process, minus a small amount of overhead. So it will give you a decent idea of actual memory usage. The 'sort -nr' sorts the ps output numerically in reverse order (so the biggest memory hogs are at the top), and we limit the display to just the top ten.
If you are maintaining a multi-user system, you can add the process owner data to the display:
Which now looks like this:
To get a constantly updating display, use the 'watch' command and specify how often you would like it to update, in this case every 10 seconds:
This will clear your terminal and add a header line with the time interval, command name and a timestamp. Note that this works on any flavor of GNU/Linux as well as the various BSDs.
ps ax -o rss,command | sort -nr | head -n 10
Here is what the output looks like:
serenity:~# ps ax -o rss,command | sort -nr | head -n 10
35936 spamd child
24556 SCREEN -dRRaAU
16340 emacs -nw
13568 /usr/sbin/apache2 -k start
13208 /usr/sbin/apache2 -k start
13176 /usr/sbin/apache2 -k start
13112 /usr/sbin/apache2 -k start
13108 /usr/sbin/apache2 -k start
13076 /usr/sbin/apache2 -k start
13016 /usr/sbin/apache2 -k start
serenity:~#
This will show just the command name and resident set size (rss) in KB of a subset of system processes. The resident set size is the non-swapped physical memory used by each process, minus a small amount of overhead. So it will give you a decent idea of actual memory usage. The 'sort -nr' sorts the ps output numerically in reverse order (so the biggest memory hogs are at the top), and we limit the display to just the top ten.
If you are maintaining a multi-user system, you can add the process owner data to the display:
ps ax -o rss,user,command | sort -nr | head -n 10
Which now looks like this:
serenity:~# ps ax -o rss,user,command | sort -nr | head -n 10
35936 root spamd child
24556 dmaxwell SCREEN -dRRaAU
16340 dmaxwell emacs -nw
13568 www-data /usr/sbin/apache2 -k start
13208 www-data /usr/sbin/apache2 -k start
13176 www-data /usr/sbin/apache2 -k start
13112 www-data /usr/sbin/apache2 -k start
13108 www-data /usr/sbin/apache2 -k start
13076 root /usr/sbin/apache2 -k start
13016 www-data /usr/sbin/apache2 -k start
serenity:~#
To get a constantly updating display, use the 'watch' command and specify how often you would like it to update, in this case every 10 seconds:
watch -n 10 'ps ax -o rss,user,command | sort -nr | head -n 10'
This will clear your terminal and add a header line with the time interval, command name and a timestamp. Note that this works on any flavor of GNU/Linux as well as the various BSDs.
Every 10.0s: ps ax -o rss,user,command | sort -nr | head -n 10 Fri Mar 2 11:08:40 2012
35936 root spamd child
24556 dmaxwell SCREEN -dRRaAU
16340 dmaxwell emacs -nw
13568 www-data /usr/sbin/apache2 -k start
13208 www-data /usr/sbin/apache2 -k start
13176 www-data /usr/sbin/apache2 -k start
13112 www-data /usr/sbin/apache2 -k start
13108 www-data /usr/sbin/apache2 -k start
13076 root /usr/sbin/apache2 -k start
13016 www-data /usr/sbin/apache2 -k start