How to see top processes sorted by actual memory usage on Linux?


Linux provides the famous top command utility that provides us with all the information about the processes, their time, their respective IDs, how much CPU chunk they are consuming and much more. The only issue with that is the processes are not sorted in any order and the order changes frequently.

There are certain cases where we would like the output to be in a sorted manner somehow, like sorted in the sense that the process which is using the most memory will be at the top.

The most basic approach is to make use of the ps command that Linux provides and append similar commands with it, mainly the --sort tag and the mem command.

Let’s learn a little bit about the ps command before making use of it to print the processes that are consuming the most memory.

The first command is the ps command of the Linux operating system. The ps command is short for “process status” and is used to list the currently running processes on your Linux machine along with their PIDs and different options.

Syntax

ps ef | [ - options ]

Let’s make use of two examples where first we will print all the processes that are running on your ubuntu machine, and next we will print whether a specific service is running or not.

Command

ps ef

Output

UID PID PPID C  STIME  TTY    TIME        CMD
 0   1   0   0  25Jun21 ??  19:53.44  /sbin/launchd
 0  56   1   0  25Jun21 ??   0:41.68  /usr/sbin/syslogd
 0  57   1   0  25Jun21 ??   0:59.28  /usr/libexec/UserEventAgent (System)

The output of the above command is huge, so I printed only a chunk of it, and it should be noted that the output can vary.

Now that we know a little about the ps command, let’s see the command that will give us the desired output.

Command

ps aux --sort ‘%mem’

Output

USER     PID %CPU %MEM   VSZ     RSS  TTY
...
root    1284 1.5  3.7  452692  142796 tty7
immukul 2286 0.3  3.8 1316000  143312 ?
immukul 5150 0.0  4.4  660620  168488 pts/0
immukul 5147 0.0  4.5  660556  170920 pts/0
immukul 5142 0.1  6.3 2581944  239408 pts/0
immukul 2386 3.6 16.0 1752740  605372 ?

As we can see in the above output the processes are sorted in increasing order of the memory consumed by them.

An alternate option is to make use of the top command itself and following the steps mentioned below

After typing top in your terminal, press SHIFT + M, then sort the MEM per process in the interactive menu. Finally, run the command shown below

top -o +%mem

Output

USER     PID %CPU %MEM  VSZ    RSS   TTY
...
root    1284 1.5  3.7 452692  142796 tty7
immukul 2286 0.3  3.8 1316000 143312 ?
immukul 5150 0.0  4.4 660620  168488 pts/0
immukul 5147 0.0  4.5 660556  170920 pts/0
immukul 5142 0.1  6.3 2581944 239408 pts/0
immukul 2386 3.6 16.0 1752740 605372 ?

Updated on: 30-Jul-2021

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements