Show total memory usage by each application in your Ubuntu (or any GNU Linux) system
BY webmaster
6 years ago
4 comments comment

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.

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



on 04th April 2007 / by webmaster
Linux is the epitome of the FLOSS model. Technical Solution Linux installation Dewey J. Corl (not verified) access_time 20 Sep 2021 - 18:02 Linux is not ready for the general public! (I am a Ubuntu user). For proof, 1. try to simply print labels on a Dymo label printer. It takes a LOT of work to get to the ease of label printing already available in Windows and Mac. 2. try syncing a modern Palm based PDA. Yes, jpilot and others will sync the main data, but pictures and midi files do not get transferred without a lot of extra setup. These are examples of applications that are not ready for the general public. Since an operating system only supports applications, Linux is not ready to be a common desktop for the general public. While we are waiting for the applications to catch up, keep up the good work!! Anoop John (not verified) access_time 20 Sep 2021 - 18:02 Dewey Yes you may be right. But these are not applications that a common man would use. A common man would use one of these applications - Internet browser, Chat Client, Email Client, Document Writer, Presentation Creator, Spreadsheet Editor, Movie Viewer, Music Player, CD/DVD Writer. Both examples you cited are more specific applications that only a small percentage of the whole population uses. It will take time before those hardware vendors identify the need from their perspective to address the Linux community. Krishnadas (not verified) access_time 20 Sep 2021 - 18:02 Dear Mr Anoop, Thank you very much for the free installation of LINUX done in my PC. I am getting immersed in the LINUX and seeing the versatility. To my surprise, I am able to read one of my old backup CD(wherein lot of my valuable file exist) done in DIRECT CD wizard (a custom cd writing software of Easy Cd creator in WIN98) which could not be read in XP. Installation of old version of easycd creator/direct cd program was not possible in XP. I thought that possible i lost all data. Very pleasant start! Srikanth N. S. (not verified) access_time 20 Sep 2021 - 18:02 Hi Anoop, My friend is thrilled with Ubuntu. One of his grouses with windows was that after OS installation, everything else is to be installed separately which is a real headache. WHereas if u install Linux, everything is installed as a package and he is thrilled to bits! Let linux installation spread in Trivandrum and your service in this direction is highly appreciated. Kepp up the good work Regards. PDA Freak (not verified) access_time 20 Sep 2021 - 18:02 Is it possible to install linux on a packardbell notebook ? webmaster access_time 20 Sep 2021 - 18:02 In reply to Is it possible to install by PDA Freak (not verified) Sir, It should be possible. Try installing and if it shows any issues please inform us. May be we could help you. Thank You Team Zyxware



for (j = 5; human_readable < 1; j--)
human_readable = size_in_bytes / (2^(10*j))

division at this line fails when size_in_bytes is 0 with the following error, we could move this loop to a new else part of the succeeding if condition

awk: cmd. line:9: (FILENAME=- FNR=221) fatal: division by zero attempted

corrected version -