Show total memory usage by each application in your Ubuntu (or any GNU Linux) system
https://www.zyxware.com/sites/default/files/styles/user_image/public/pictures/zyxlogo.png?itok=J9t-ZEoU
BY webmaster
4 years ago
Shell-Scripting
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.

#!/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


RELATED ARTICLE

more_horiz
close

on 11th December 2008 / by webmaster
Recently we had to provide support on a Red Hat Server where there were hundreds of users and the user directories organized into hierarchies. Normally we change to a users directory using cd ~username. We had some extra time on our hands and wanted to play with the /etc/passwd file and tried to use awk to do the same thing. The following one liner will do this the hard way :-) Linux Shell Scripting Bash Leave a reply Your email address will not be published. Required fields are marked * cherry (not verified) access_time 22 Aug 2019 - 13:48 alternatively: cd `(cat /etc/passwd|grep username|cut -d ':' -f 6` ;-) Add new comment
close

on 12th December 2009 / by webmaster
Asianet Dataline is an ISP in Kerala and they use a web browser based authentication system to lo Internet Shell Scripting Bash Asianet Leave a reply Your email address will not be published. Required fields are marked * Vivek Thomas (not verified) access_time 22 Aug 2019 - 13:48 Thank you very much for this. Works great. Btw I had to add a --no-check-certificate option in the following line for it get the login URL. :) asianet_conn_url=`wget --quiet --no-check-certificate -O - www.zyxware.com|grep 'action='|sed 's/\(.*action="\)\(.*\)"&gt;/\2/g'` webmaster access_time 22 Aug 2019 - 13:48 In reply to Thank you very much for this. by Vivek Thomas (not verified) Changes duly made in git repo and credit given. Anoop JITHIN (not verified) access_time 22 Aug 2019 - 13:48 In reply to Thanks by webmaster Sir, Where is this "Favorite Scripts Folder" How Can I Copy A Script ie In Text Form To A Folde? Is Favorite Script Folder an attachment Of Internet Explorer Browser ? Please Tell Me About "START UP PROCESS" Can You Plese Explain What Should I Do Step By Step Considering Me As A Beginer. Jimmy (not verified) access_time 22 Aug 2019 - 13:48 Hi, I see the script. Can I use this script in Windows? If yes, please tell me the complete procedure to use this script in windows using the browser IE. Thanks Jimmy Anonymous (not verified) access_time 22 Aug 2019 - 13:48 Thank you very much for this. Works great. But I cant logout after use Add new comment
more_horiz
close

on 28th January 2012 / by webmaster
We had sometime back released an Utilities Internet Free Software Shell Scripting Reliance Python Leave a reply Your email address will not be published. Required fields are marked * Praveen Pious (not verified) access_time 22 Aug 2019 - 14:45 Not everyone is well versed with running python scripts on their windows machines. I downloaded and installed python off from their website. I set the environment variable correctly too. Yet, I am facing difficulty while trying to get this script working. Double clicking the script just makes a black command prompt prompt window to appear. Nothing seems to be happening on the background. So, please help me out. What am I doing wrong here? Anonymous (not verified) access_time 22 Aug 2019 - 14:45 i am using a connect broadband. and i don't know how to use a script. please tell me how use it? thanks in advance. Dhruv (not verified) access_time 22 Aug 2019 - 14:45 Even i'm having trouble getting this script working. Can you please tell this in layman terms? Hellobye80 (not verified) access_time 22 Aug 2019 - 14:45 from where can we download this free software. can you please share the link? Ethan Stone (not verified) access_time 22 Aug 2019 - 14:45 Is this script still working for you? Pagination Current page 1 Page 2 Next page Next › Last page Last » Add new comment
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

author-image

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 - https://gist.github.com/ershad/428514b68b4bdf2c570d