178

I am able to see the list of all the processes and the memory via

ps aux 

and going through the VSZ and RSS

Is there a way to sort down the output of this command by the descending order on RSS value?

3

9 Answers 9

289

Use the following command:

ps aux --sort -rss

Check here for more Linux process memory usage

13
  • 18
    note - if you want to see the top results it's helpful to pipe it into head like ps aux --sort -rss | head -n15
    – Yehosef
    Commented Mar 26, 2015 at 15:38
  • 3
    I get error ps: illegal option -- - Commented May 19, 2016 at 18:27
  • @MiguelMota What if like this: ps aux --sort=rss ?
    – Vombat
    Commented May 19, 2016 at 18:32
  • 4
    @coffeMug didn't work but this did ps aux | sort -rn -k 6 Commented May 19, 2016 at 18:41
  • 1
    @coffeMug not sure how to find the version but I'm on Mac OSX so maybe that's why Commented May 19, 2016 at 18:58
30

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
6
  • 1
    will ps always output the columns in the way you expect sort to see/process them? Commented Aug 13, 2015 at 2:34
  • Depends on which version of ps
    – slm
    Commented Aug 13, 2015 at 4:04
  • 3
    A ... | less is a good advice but sometimes your process has a huge command line and it clutters the output. In such cases ... | less -S works better.
    – waste
    Commented Nov 17, 2016 at 23:54
  • 1
    @waste - good tip, just remember that -S truncates and so you may lose some of what you want to see, but otherwise good advice if you're only interested the left most columns.
    – slm
    Commented Nov 18, 2016 at 2:54
  • @slm I am not sure that is the case for less -S. When you close less view everything disappears, but as long as you are in the view, you can scroll vertically but also horizontally. Copying might be difficult, though.
    – waste
    Commented Nov 18, 2016 at 3:03
12

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 "" }'
5

ps aux --sort -rss is nice:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user     5984  0.8  7.4 1632488 296056 ?      Sl   06:30   6:18 /usr/lib/chromium-browser/chromium-browser --type=ren
user    23934 21.7  6.0 1565600 241228 ?      Sl   15:45  40:10 /opt/atom/atom --type=renderer --enable-experimental-
user     5533  0.9  5.1 3154096 206376 ?      SLl  06:30   6:47 /usr/lib/chromium-browser/chromium-browser --enable-p
user    17306  1.7  4.9 1360648 196124 ?      Sl   18:14   0:36 /usr/lib/chromium-browser/chromium-browser --type=ren
user    22272 30.1  4.6 1347784 185032 ?      Sl   18:43   1:54 /usr/lib/chromium-browser/chromium-browser --type=ren
user    19318  0.6  3.3 1304324 133452 ?      Sl   18:27   0:09 /usr/lib/chromium-browser/chromium-browser --type=ren
user    22098  1.0  3.3 1298500 133216 ?      Sl   18:43   0:04 /usr/lib/chromium-browser/chromium-browser --type=ren

but if you want to see memory and cpu usages by application (grouped by commands):

python3.6  sum_process_resources.py 
====   CPU%   ====
0. /opt/atom/atom | 27.8
1. /usr/lib/chromium-browser/chromium-browser | 11.2
2. python3.6 | 11.0
3. /opt/google/chrome/chrome | 1.6
4. /usr/lib/xorg/Xorg | 1.4
5. /opt/Franz/franz | 0.7
====   MEM%   ====
0. /usr/lib/chromium-browser/chromium-browser | 37.2
1. /opt/google/chrome/chrome | 11.3
2. /opt/Franz/franz | 10.6
3. /opt/atom/atom | 10.1
4. /usr/lib/xorg/Xorg | 2.0
5. com.google.android.gms.persistent | 1.4
====   RSS MB   ====
0. /usr/lib/chromium-browser/chromium-browser | 1475.07 MB
1. /opt/google/chrome/chrome | 461.35 MB
2. /opt/Franz/franz | 429.04 MB
3. /opt/atom/atom | 402.18 MB
4. /usr/lib/xorg/Xorg | 78.53 MB
5. com.google.android.gms.persistent | 58.02 MB

code:

#sum_process_resources.py
from collections import OrderedDict
import subprocess

def run_cmd(cmd_string):
    """Runs commands and saves output to variable"""
    cmd_list = cmd_string.split(" ")
    popen_obj = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
    output = popen_obj.stdout.read()
    output = output.decode("utf8")
    return output

def sum_process_resources():
    """Sums top X cpu and memory usages grouped by processes"""
    ps_memory, ps_cpu, ps_rss = {}, {}, {}
    top = 6
    output = run_cmd('ps aux').split("\n")
    for i, line in enumerate(output):
        cleaned_list = " ".join(line.split())
        line_list = cleaned_list.split(" ")
        if i > 0 and len(line_list) > 10:
            cpu = float(line_list[2])
            memory = float(line_list[3])
            rss = float(line_list[5])
            command = line_list[10]
            ps_cpu[command] = round(ps_cpu.get(command, 0) + cpu, 2)
            ps_memory[command] = round(ps_memory.get(command, 0) + memory, 2)
            ps_rss[command] = round(ps_rss.get(command, 0) + rss, 2)
    sorted_cpu = OrderedDict(sorted(ps_cpu.items(), key=lambda x: x[1], reverse=True))
    sorted_memory = OrderedDict(sorted(ps_memory.items(), key=lambda x: x[1], reverse=True))
    sorted_rss = OrderedDict(sorted(ps_rss.items(), key=lambda x: x[1], reverse=True))
    print("====   CPU%   ====")
    for i, k in enumerate(sorted_cpu.items()):
        if i < top:
            print("{}. {} | {}".format(i, k[0], k[1]))
    print("====   MEM%   ====")
    for i, k in enumerate(sorted_memory.items()):
        if i < top:
            print("{}. {} | {}".format(i, k[0], k[1]))
    print("====   RSS MB   ====")
    for i, k in enumerate(sorted_rss.items()):
        if i < top:
            print("{}. {} | {} MB".format(i, k[0], round((k[1]/1024), 2)))


if __name__ == '__main__':
    sum_process_resources()
2

simple way is to install htop

in that you can sort process based on PID,Percentage CPU,MEM

more sophisticated

1

As an alternative to the BSD style arguments shown in the other answers, one can use (at least using procps, shipped by Debian and Ubuntu):

ps -eF --sort=-rss
1
  1. Run top command
  2. Shift + F to sort based on field (see the full menu below)
  3. Select n to sort based on memory usage

n: %MEM = Memory usage (RES)

1

How to total up used memory by process name:

Sometimes even looking at the biggest single processes there is still a lot of used memory unaccounted for. To check if there are a lot of the same smaller processes using the memory you can use a command like the following which uses awk to sum up the total memory used by processes of the same name:

ps -e -orss=,args= |awk '{print $1 " " $2 }'| awk '{tot[$2]+=$1;count[$2]++} END {for (i in tot) {print tot[i],i,count[i]}}' | sort -n

e.g. output

9344 docker 1
9948 nginx: 4
22500 /usr/sbin/NetworkManager 1
24704 sleep 69
26436 /usr/sbin/sshd 15
34828 -bash 19
39268 sshd: 10
58384 /bin/su 28
59876 /bin/ksh 29
73408 /usr/bin/python 2
78176 /usr/bin/dockerd 1
134396 /bin/sh 84
5407132 bin/naughty_small_proc 1432
28061916 /usr/local/jdk/bin/java 7
0
ps aux --sort -%mem  

man ps will show you many usages.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .