Sorting down processes by memory usage

Use the following command:

ps aux --sort -rss

Check here for more Linux process memory usage


A quick and dirty method is to just pipe the output of ps aux to the sort command:

$ ps aux | sort -rn -k 5,6

Example

$ ps aux | sort -rn -k 5,6
...
root      1584  0.0  0.0  22540  1236 ?        S    07:04   0:01 hald-addon-storage: polling /dev/sr0 (every 2 sec)
root      1575  0.0  0.0  22536   872 ?        S    07:04   0:00 /usr/libexec/hald-addon-generic-backlight
root      1574  0.0  0.0  22536   880 ?        S    07:04   0:00 /usr/libexec/hald-addon-leds
root      1565  0.0  0.0  22536   876 ?        S    07:04   0:00 /usr/libexec/hald-addon-rfkill-killswitch
saml      2507  0.0  0.0  22232   500 ?        S    07:05   0:00 dbus-launch --sh-syntax --exit-with-session
root      1671  0.0  0.0  22156   936 ?        Ss   07:04   0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
...

This doesn't handle for the column headers which get mixed in with the output, but it's easy to remember on the command line, and is an acceptable way to do what you want when manually viewing this type of output.

Example

root      1791  0.0  0.0   4140   536 tty2     Ss+  07:04   0:00 /sbin/mingetty /dev/tty2
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       996  0.0  0.0      0     0 ?        S    07:04   0:01 [kdmflush]
root       982  0.0  0.0      0     0 ?        S    07:04   0:00 [kvm-irqfd-clean]

More tips

An additional tip would be to pipe the entire output to another command such as less. This allows you to look at the information a page at a time and also use the arrow keys and page up/down keys to scroll back and forth through the output.

$ ps aux | sort -rn -k 5,6 | less

If your output is wrapping a lot you can also utilize the -S switch to less, which will force all the output to stay on a single line instead. You can then use your arrow keys to move left/right/up/down to see all of it.

$ ps aux | sort -rn -k 5,6 | less -S

Sorting within ps

Certain versions of ps provide the ability to use --sort. This switch can then take keys that are either prefixed with a + or a - to denote the sort order...least to greatest or greatest to least.

Examples

vsz,-rss

$ ps aux --sort=vsz,-rss | head -5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         2  0.0  0.0      0     0 ?        S    07:03   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    07:03   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    07:03   0:01 [migration/0]
root         5  0.0  0.0      0     0 ?        S    07:03   0:00 [watchdog/0]

+vsz,+rss

$ ps aux --sort=+vsz,+rss | head -5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         2  0.0  0.0      0     0 ?        S    07:03   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    07:03   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    07:03   0:01 [migration/0]
root         5  0.0  0.0      0     0 ?        S    07:03   0:00 [watchdog/0]

-vsz,-rss

$ ps aux --sort=-vsz,-rss | head -5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1832  0.0  0.0 2088924 3312 ?        Sl   07:04   0:00 /usr/sbin/console-kit-daemon --no-daemon
saml      3517  0.2  1.2 2073196 100492 ?      Sl   07:06   0:34 /home/saml/.dropbox-dist/dropbox
saml      3516  0.0  0.8 2071032 67388 ?       Sl   07:06   0:07 /home/saml/.dropbox-dist/dropbox
saml      2657  0.1  0.7 1580936 57788 ?       Sl   07:05   0:27 nautilus

Even if ps do not reflect the actual memory used, this command is pretty helpful.

ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'

Tags:

Linux

Memory