Linux is an open-source and highly popular operating system, and it is the preferred option for deploying servers and applications on the Internet. It is very popular due to its command-line interface and built-in tools which help you to perform your tasks quickly and save a lot of time.

Sometimes your application or website is very slow or unresponsive due to high memory and CPU usage. In this case, you can use the ps and top command to identify which processes are eating all the resources on your system.

In this post, we will explain how to find top memory and CPU resource-consuming processes in Linux.

Use ps Command to Find Top Processes

ps is a Linux command-line utility with many options that helps you to display output in different formats.

To list all running processes in Linux, run the following command:

ps aux

This will show you an overview of all running processes:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.1 160716  9904 ?        Ss   16:58   0:06 /sbin/init splash
root         2  0.0  0.0      0     0 ?        S    16:58   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        I<   16:58   0:00 [rcu_gp]
root         4  0.0  0.0      0     0 ?        I<   16:58   0:00 [rcu_par_gp]
root         6  0.0  0.0      0     0 ?        I<   16:58   0:00 [kworker/0:0H-kb]
root         9  0.0  0.0      0     0 ?        I<   16:58   0:00 [mm_percpu_wq]
root        10  0.0  0.0      0     0 ?        S    16:58   0:00 [ksoftirqd/0]
root        11  0.1  0.0      0     0 ?        I    16:58   0:05 [rcu_sched]
root        12  0.0  0.0      0     0 ?        S    16:58   0:00 [migration/0]

You can use the ps command with –sort argument to sort the output by memory and CPU usage.

The syntax for using the sort argument is:

ps aux --sort

To find the top running processes by CPU usage, run the following command:

ps aux --sort -%cpu

Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vyom      8115  7.8  3.0 25770108 233784 tty2  Sl+  17:58   1:37 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=82 --launch-time-ticks=3564377766 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      8164  6.2  3.0 25705000 233456 tty2  Sl+  17:58   1:16 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=85 --launch-time-ticks=3576904510 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      2806  4.4  4.5 17565904 343660 tty2  SLl+ 17:01   3:26 /opt/google/chrome/chrome --enable-crashpad
vyom      2314  4.3  2.9 3472696 222248 tty2   Rl+  17:00   3:22 /usr/bin/gnome-shell

To find the top running processes by memory usage, run the following command:

ps aux --sort -%mem

Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
vyom      2806  4.4  4.5 17562832 343688 tty2  SLl+ 17:01   3:26 /opt/google/chrome/chrome --enable-crashpad
vyom      8115  7.8  3.0 25770108 232204 tty2  Sl+  17:58   1:38 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=82 --launch-time-ticks=3564377766 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport
vyom      8164  6.1  3.0 25705000 230116 tty2  Sl+  17:58   1:16 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=2815 --enable-crash-reporter=373d0de2-e0c8-419c-b983-084c773fcd79, --display-capture-permissions-policy-allowed --change-stack-guard-on-fork=enable --lang=en-GB --num-raster-threads=1 --renderer-client-id=85 --launch-time-ticks=3576904510 --shared-files=v8_context_snapshot_data:100 --field-trial-handle=0,i,7343938639469663677,16234295293987540603,131072 --enable-features=PasswordImport

If you want to show only the top 10 memory consuming processes, run the following command:

ps aux --sort -%mem | head -10

If you want to show only the command name instead of the full path of the command, run the following command:

ps -eo pid,ppid,cmd,comm,%mem,%cpu --sort=-%mem | head -10

Output:

  PID  PPID CMD                         COMMAND         %MEM %CPU
 2806     1 /opt/google/chrome/chrome - chrome           4.5  4.3
 8164  2828 /opt/google/chrome/chrome - chrome           3.2  6.2
 8115  2828 /opt/google/chrome/chrome - chrome           3.0  7.8
 2314  2176 /usr/bin/gnome-shell        gnome-shell      2.9  4.4
 2996  2828 /opt/google/chrome/chrome - chrome           2.4  0.5
 8074  2828 /opt/google/chrome/chrome - chrome           2.4  0.8
 7520  2828 /opt/google/chrome/chrome - chrome           2.4  0.3
 8175  2828 /opt/google/chrome/chrome - chrome           2.2  0.3
 2858  2823 /opt/google/chrome/chrome - chrome           2.2  3.8

Also Read

How to Check Linux CPU Usage or Utilization

Use the top Command to Find Top Processes by Memory and CPU Usage

top is another built-in Linux command-line utility that can be used to show all running processes in Linux. You can use various options with the top command to filter the output based on your requirements.

You can use the top command with the -o flag to show the top memory consuming processes:

top -o %MEM

Output:

Tasks: 329 total,   1 running, 281 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.5 us,  0.7 sy,  0.0 ni, 94.6 id,  3.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  7580260 total,  2602168 free,  2668376 used,  2309716 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4486960 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2806 vyom      20   0 16.753g 343596 183124 S   0.0  4.5   3:37.39 chrome                                                                    
 8164 vyom      20   0 24.516g 241968 103164 S   0.0  3.2   1:44.22 chrome                                                                    
 8115 vyom      20   0 24.570g 237756 113464 S   0.0  3.1   2:04.79 chrome                                                                    
 2314 vyom      20   0 3472696 222248  97556 S   1.3  2.9   3:49.13 gnome-shell                                                               
 8074 vyom      20   0 24.508g 187804 110344 S   0.0  2.5   0:11.59 chrome                                                                    
 7520 vyom      20   0 24.563g 185760 104852 S   0.0  2.5   0:06.98 chrome                                                                    
 2996 vyom      20   0 24.503g 185316  85720 S   0.0  2.4   0:27.90 chrome                                                                    
 8175 vyom      20   0 24.518g 171224 100040 S   0.0  2.3   0:04.96 chrome                                                                    

If you want to display only the top 10 memory consuming processes, run the following command:

top -o %MEM | head -n 16

Output:

top - 18:31:11 up  1:32,  1 user,  load average: 0.32, 0.41, 0.65
Tasks: 330 total,   1 running, 282 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.3 us,  3.5 sy,  0.1 ni, 78.8 id,  3.1 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem :  7580260 total,  2623576 free,  2655868 used,  2300816 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4508812 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2806 vyom      20   0 16.749g 343920 183192 S   0.0  4.5   3:39.02 chrome                                                                    
 8164 vyom      20   0 24.513g 232772 103276 S   0.0  3.1   1:45.33 chrome                                                                    
 8115 vyom      20   0 24.557g 230336 111896 S   0.0  3.0   2:05.62 chrome                                                                    
 2314 vyom      20   0 3472696 222880  97560 S   0.0  2.9   3:59.60 gnome-shell                                                               
 8074 vyom      20   0 24.508g 187740 110344 S   0.0  2.5   0:11.61 chrome                                                                    
 7520 vyom      20   0 24.563g 185724 104852 S   0.0  2.5   0:06.99 chrome                                                                    
 2996 vyom      20   0 24.503g 185012  85720 S   0.0  2.4   0:27.98 chrome                                                                    
 8175 vyom      20   0 24.518g 171224 100040 S   0.0  2.3   0:04.97 chrome                                                                    
 2735 vyom      20   0 37.371g 168668 119056 S   0.0  2.2   0:05.57 skypeforlinux                                                             

If you want to display only the top 10 CPU-consuming processes, run the following command:

top -o %CPU | head -n 16

Output:

top - 18:32:05 up  1:33,  1 user,  load average: 0.48, 0.43, 0.64
Tasks: 330 total,   1 running, 282 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.2 us,  3.5 sy,  0.1 ni, 78.9 id,  3.1 wa,  0.0 hi,  0.1 si,  0.0 st
KiB Mem :  7580260 total,  2621204 free,  2662180 used,  2296876 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  4506588 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                   
 2314 vyom      20   0 3472696 223296  97560 S  11.8  2.9   4:03.68 gnome-shell                                                               
 2161 vyom      20   0  998276  86120  55136 S   5.9  1.1   2:30.95 Xorg                                                                      
 8822 vyom      20   0   44368   4188   3364 R   5.9  0.1   0:00.02 top                                                                       
    1 root      20   0  160716   9904   6644 S   0.0  0.1   0:06.65 systemd                                                                   
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kthreadd                                                                  
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp                                                                    
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp                                                                
    6 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 kworker/0:0H-kb                                                           
    9 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq                                                              

Also Read

How to Check Size of Files and Directory on Linux

Conclusion

In this post, we explained how to find top CPU and Memory consuming processes in Linux. You can use these commands to troubleshoot performance-related issues in Linux. Try it on VPS hosting from Atlantic.Net!