16

How can we sum the memory usages of all the processes of a program?

For example, I have opened many Chrome tabs for webpages, and each tab runs a different process. How can I get the total sum of memory usages of all the Chrome tabs? Note that the executable file for Chrome is /opt/google/chrome/chrome on my Ubuntu.

3

4 Answers 4

12

One handy way is to use atop. In atop, you can type P to see per program statistics like this:

                                                                         PAUSED
NPROCS  SYSCPU  USRCPU   VSIZE  RSIZE   RDDSK  WRDSK  SNET  MEM CMD         1/4
    17   1.14s   8.06s   14.3G   2.7G       ?      ?     ?  35% chrome
     1   0.30s   0.30s    2.6G   1.9G       ?      ?     ?  25% Xorg
     1   0.09s   1.15s    1.8G 329.7M       ?      ?     ?   4% gnome-shell
     1   0.00s   0.07s  994.8M 257.2M       ?      ?     ?   3% thunderbird
     1   0.00s   0.00s    1.3G 162.4M       ?      ?     ?   2% soffice.bin
     2   0.00s   0.00s    1.2G 86736K       ?      ?     ?   1% gvim
     1   0.00s   0.01s    1.0G 64724K       ?      ?     ?   1% owncloud
     1   0.00s   0.00s  970.3M 59908K       ?      ?     ?   1% evolution-cale
     1   0.00s   0.00s  675.7M 48404K       ?      ?     ?   1% tracker-extrac
     1   0.03s   0.03s  581.0M 47080K       ?      ?     ?   1% xchat
     2   0.00s   0.00s   84.2G 45292K       ?      ?     ?   1% nacl_helper

Type M for per process memory stats.

                                                                         PAUSED
  PID MINFLT  MAJFLT VSTEXT   VSIZE  RSIZE   VGROW  RGROW   MEM  CMD        1/5
 1069  38118       0     0K    2.6G   1.9G      0K    40K   25%  Xorg
14702     20       0 91595K  939.1M 427.0M      0K     0K    5%  chrome
 3755     39       0    11K    1.8G 329.7M      0K     0K    4%  gnome-shell
14669   7804       0 91595K    1.8G 293.1M      0K  -136K    4%  chrome
15530    477       0 91595K    1.1G 292.6M  -8196K  -156K    4%  chrome
 3932      6       0    96K  994.8M 257.2M      0K     0K    3%  thunderbird
15436   7434       0 91595K  978.4M 228.5M  -1024K  -212K    3%  chrome
14821   2129       0 91595K    1.0G 220.2M   1024K  1936K    3%  chrome
15084    213       0 91595K  890.7M 211.9M      0K     0K    3%  chrome
15129     58       0 91595K  915.4M 208.1M      0K     0K    3%  chrome
14729     13       0 91595K    1.1G 188.0M      0K     0K    2%  chrome
15474   2080       0 91595K  858.5M 166.0M      0K  -800K    2%  chrome
11220      0       0     2K    1.3G 162.4M      0K     0K    2%  soffice.bin

Type H to get a brief help for avaiable commands. Type A to toggle stats collection for active-only/all process, Z to pause automatic updates before your investigation (PAUSED seen in top right when enabled).

2
  • 1
    This might not give the correct result for many commands. For instance, firefox launches many Web Content processes (superuser.com/questions/1299746/…), and these would show separately. There is no a priori way to tell which to sum. And chrome might be doing the same. Sep 16, 2021 at 10:05
  • any solution to group all processes from one program? like chrome
    – TomSawyer
    Jan 31 at 2:42
7

You can use the ps command with additional stuff like this:

ps -eo size,command --sort -size | grep chromium | awk '{ hr=$1/1024 ; sum +=hr} END {print sum}'

For daily use case, make a file (I named it memsum here!) and put it :

ps -eo size,command --sort -size | grep $1 | awk '{ hr=$1/1024 ; sum +=hr; echo $1} END {print sum}'
#                                       ^this $1 will come from runtime argument

Then set x flag on it (to make it executable):

chmod +x memsum

And use it:

./memsum chromium
./memsum httpd
1
  • 3
    You can improve this script by adding the unit (i.e. MiB).
    – Creak
    May 27, 2020 at 19:50
1

The following command greps output of ps, prints that output, and sums values in a column using awk. It's useful to see the output of ps as well as the sum, to verify the right set of processes are being summed.

ps xuaw --cols=80|grep -e chrome -e COMMAND|grep -v grep |awk '{m=$5;sum += m;print} END {print "Total VSZ: " sum}'

Here's sample output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
2006     29273  0.0  0.3 701716 82948 ?        Sl   Oct30   1:44 /opt/google/chrome/chrome
2006     29288  0.0  0.0 351768  6892 ?        S    Oct30   0:00 /opt/google/chrome/chrome
2006     29289  0.0  0.0   6284   332 ?        S    Oct30   0:00 /opt/google/chrome/chrome-sandbox /opt/google/chrome/chrome --type=
2006     29290  0.0  0.0 398284 20104 ?        S    Oct30   0:00 /opt/google/chrome/chrome --type=zygote
2006     29294  0.0  0.0 125500  4684 ?        S    Oct30   0:00 /opt/google/chrome/nacl_helper_bootstrap /opt/google/chrome/nacl_he
2006     29295  0.0  0.0 406480  7300 ?        S    Oct30   0:00 /opt/google/chrome/chrome --type=zygote
2006     29432  0.0  0.0 989216 19464 ?        Sl   Oct30   0:01 /opt/google/chrome/chrome --type=renderer --lang=en-US --force-fiel
2006     29772  0.0  0.1 1004072 32408 ?       Sl   Oct30   0:09 /opt/google/chrome/chrome --type=renderer --lang=en-US --force-fiel
2006     29780  0.0  0.0 389592 19404 ?        Sl   Oct30   0:00 /opt/google/chrome/chrome --type=service
2006     29788  0.0  0.0 938204 21080 ?        Sl   Oct30   0:00 /opt/google/chrome/chrome --type=ppapi-broker --channel=29273.5.113
Total VSZ: 5311116
-1

You can use qps. It shows the total usage of each process.

            ss#1

Reference

1
  • 6
    But the OP want to sum up all the memory usage... Jun 15, 2015 at 4:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .