It is quite a common situation when your server is out of memory and you want to check what processes are using all the RAM and swap.
In this small note you’ll find two similar commands that can find out and sort top processes by memory usage on your Linux system.
I’ve successfully used these commands on: Linux Mint, Ubuntu, Debian, CentOS, RHEL.
Use any of the following commands to display top 10 processes (including child processes) that use the most memory on your Linux machine.
Show What Processes Are Using All The RAM in Linux
Use the following command to find out top processed sorted by memory usage, in megabytes (MB):
ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'
Example: Just copy/paste the above command to your terminal and press ENTER to display top 10 processes, sorted by memory usage. You should get the similar output:
admin@phantom ~ $ ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'
1047MB firefox
648MB VirtualBox
177MB thunderbird
119MB Xorg
82MB python
82MB remmina
60MB pidgin.orig
48MB caja
47MB apache2
33MB puppet
Display Processes Sorted By Memory Usage in Linux
Use the following command to display processes that are using all the memory, in megabytes (MB):
ps axo rss,comm,pid \
| awk '{ proc_list[$2] += $1; } END \
{ for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc],proc); }}' \
| sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'
Example: Just copy/paste the above command to your terminal and press ENTER to check what processes are using all the RAM and swap. You should get the similar output:
admin@phantom ~ $ ps axo rss,comm,pid \
| awk '{ proc_list[$2] += $1; } END \
{ for (proc in proc_list) { printf("%d\t%s\n", proc_list[proc],proc); }}' \
| sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'
1022MB firefox
648MB VirtualBox
177MB thunderbird
119MB Xorg
82MB python
82MB remmina
60MB pidgin.orig
48MB caja
47MB apache2
33MB puppet
Wow, this is brilliant, thank you
Merci, thank you very much!