Show total memory usage by each application in your Ubuntu (or any GNU Linux) system
If you are a GNU Linux user it is very likely that you would already know about the command top that allows you to see the memory and processor usage by each process in your system. However you would see multiple processes for applications like Chromium or Firefox for each of the tabs that are open in them. Similarly you could see apache run multiple processes if you have it configured to do so. What if you want to see the total memory usage for each application and not by each process. Here is a small bash script to help you do the same.
Copy the following and save as ls-mem (or whatever name you wish to name it as) and then add it to a folder which is in your $PATH
variable. It could be ~/bin
or /usr/local/bin
based on whether you have permissions to these folders and based on whether the folder is in the $PATH
variable.
#!/bin/bash ps -A --sort -rss -o comm,pmem,rss | awk ' NR == 1 { print; next } { a[$1] += $2; b[$1] += $3; } END { for (i in a) { size_in_bytes = b[i] * 1024 split("B KB MB GB TB PB", unit) human_readable = 0 if (size_in_bytes == 0) { human_readable = 0 j = 0 } else { for (j = 5; human_readable < 1; j--) human_readable = size_in_bytes / (2^(10*j)) } printf "%-20s\t%s\t%.2f%s\t%s\n", i, a[i], human_readable, unit[j+2], b[i] } } ' | awk 'NR>1' | sort -rnk4 | awk ' BEGIN {printf "%-20s\t%%MEM\tSIZE\n", "COMMAND"} { printf "%-20s\t%s\t%s\n", $1, $2, $3 } ' | less
Make sure that you set the execute bit for the script that you have created (chmod 555 ls-mem
) and you are all set.
Happy scripting