The default output of a "ps" command is unsorted by default. However, this default behavior can be changed with use of --sort or "k" options. In this short tutorial we will show how to sort processes based on memory usage.

Let's start with a simple ps command output:
root      1354  1.9  0.4 220900 37780 tty1     Ss+  07:18   7:05 /usr/bin/Xorg
lrendek   8803  0.0  0.0 116536  3260 pts/3    Ss+  09:54   0:00 /bin/bash
lrendek   8885  0.0  0.0 116668  3480 pts/4    Ss+  09:59   0:00 /bin/bash
lrendek   9294  0.0  0.0 116536  3320 pts/5    Ss   10:01   0:00 /bin/bash
root     11633  0.0  0.0 200656  3616 pts/5    S    11:18   0:00 su
lrendek   2709  9.5  9.1 2114284 739140 ?      Sl   07:21  32:39 /usr/lib64/firefox/firefox
lrendek  12300  0.0  0.0 116536  3260 pts/1    Ss   11:25   0:00 /bin/bash
lrendek  12341  0.0  0.0 110272  1184 pts/1    S+   11:25   0:00 less -s
lrendek  12353  0.0  0.0 116536  3196 pts/2    Ss   11:26   0:00 /bin/bash
As it was already mentioned previously the default ps command output is unsorted. However, ps allows to sort its output based on any column value. To sort by memory usage we can use either "%MEM" or "RSS" columns. The RSS ( Resident Set Size ) is a total memory usage in kilobytes and "%RAM" shows the same information in terms of percent usage of total memory amount available. What follows are few examples on how to instruct ps command to sort by memory usage:

1. Sort by %MEM

Sort by RAM percent usage. Highest values first:
$ ps au --sort=-%mem
$ ps auk-%mem
Sort by RAM percent usage. Highest values last:
$ ps au --sort=+%mem
ps auk+%mem

2. Sort by RSS

Sort by RSS usage. Highest values first:
$ ps au --sort=-rss
$ ps auk-rss
Sort by RSS usage. Highest values last:
$ ps au --sort=+rss
$ ps auk+rss
Please note that the "+" can be omitted as it is a default option and thus making ps aukrss and ps auk+rss identical.

Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top