44

Since google chrome/chromium spawn multiple processes it's harder to see how much total memory these processes use in total.

Is there an easy way to see how much total memory a series of connected processes is using?

Ryan1729
  • 591
  • 1
  • 4
  • 7
  • If I convert the numbers that prints out from KiB to GiB then even for Res. Memory I get more than the machine's amount of RAM plus swap. So it seems like something is getting over-counted. – Ryan1729 Jun 09 '16 at 04:42

8 Answers8

48

Given that google killed chrome://memory in March 2016, I am now using smem:

# detailed output, in kB apparently
smem -t -P chrom
# just the total PSS, with automatic unit:
smem -t -k -c pss -P chrom | tail -n 1
  • to be more accurate replace chrom by full path e.g. /opt/google/chrome or /usr/lib64/chromium-browser
  • this works the same for multiprocess firefox (e10s) with -P firefox
  • be careful, smem reports itself in the output, an additional ~10-20M on my system.
  • unlike top it needs root access to accurately monitor root processes -- use sudo smem for that.
  • see this SO answer for more details on why smem is a good tool and how to read the output.
eddygeek
  • 1,261
  • 1
  • 12
  • 9
  • I am not sure that this is working for me. When I run the second command, I get a returned value of `338.0M`. This is too low. When I run System Monitor, I can see that there are 11 chrome processes and each is taking between 70MB and 400MB of RAM. Not sure if System Monitor is reporting incorrectly or not. – sixtyfootersdude Apr 05 '17 at 15:40
  • Same problem for smem on a Kali Linux 2017.1 distribution, the output is 800Mo of ram used by chrome with 5 instances and at least 30 tabs ... And the system monitor does not agree with smem either. Has anyone found a solution to this ? (Thanks for the answer and the references) – matthieusb Jun 17 '17 at 22:10
  • 1
    Your comments deserve a separate question (with full output of conflicting programs). Just post the link in the comment. – eddygeek Jun 18 '17 at 22:43
  • 1
    I would use `chrome` instead of just `chorm` because if you are running both chrome and chromium, you 'd be seeing total for both. – R J Mar 03 '18 at 12:33
  • Is there any similar solution on Mac? – Domon Apr 17 '18 at 11:14
  • 3
    You can exclude `smem` from its own output by using something like `[c]hrome`, e.g. `smem -tkP '[c]hrome'`. This works by matching the `c` inside the square brackets and not the brackets themselves. – wjandrea May 26 '18 at 04:31
  • On xfce Mint 19, chromium-browser is located in `/usr/lib/chromium-browser/` for 64bit – dotnetCarpenter Jul 21 '18 at 12:48
  • If you would like to show memory usage percentage `%` you can use `smem -t -p -c pss -P chrom | tail -n 1` – nexayq Mar 25 '19 at 11:02
  • 50MB for installing `smem` g** damn, but it works, I really like the second one, that should be the fist TL;DR thing in this answer x) – jave.web Feb 01 '22 at 23:51
4

Improving solution of @eddygeek:

smem -ktP chrome

Take a look on the value of column "USS", on last line

Note: you can also create an alias for this:

alias mem='smem -ktP '

And then use:

mem chrome
Topera
  • 476
  • 4
  • 5
3

I'm sure that it's not the best solution, still it works for me:

#!/bin/sh
ps aux | grep "[/]opt/google/chrome/chrome" | awk '{print $5}' | awk '{sum += $1 } END { print sum }' 
ps aux | grep "[/]opt/google/chrome/chrome" | awk '{print $6}' | awk '{sum += $1 } END { print sum }' 

Note: change the [/]opt/google/chrome/chrome to something appropriate for your system, e.g. if you're on Mac OS X (simply grep "chrome" will work).

Sridhar Sarnobat
  • 1,692
  • 18
  • 27
Lev Bystritskiy
  • 391
  • 1
  • 6
  • 1
    This “works” in that it prints a number. However this number is not all that useful since memory that is shared between several processes is counted multiple times. – Gilles 'SO- stop being evil' Jun 09 '16 at 22:38
  • I imagine in reality it's still good enough because Chrome is by far the biggest memory hog on typical desktops and when you kill chrome processes your system becomes blazing fast. – Sridhar Sarnobat Dec 03 '16 at 02:48
  • Why two `awk` commands? That is, why not just `... | awk '{sum += $6} END {print sum}'`? – wjandrea Apr 23 '18 at 06:37
  • 2
    FWIW, here's a shorter, clearer version: `ps aux | grep "/opt/google/chrome/chrome" | awk '{vsz += $5; rss += $6} END { print "vsz="vsz, "rss="rss }'` – wjandrea Apr 23 '18 at 17:24
  • Note that the square brackets in the `grep` command are supposed to avoid matching the pipeline itself. – wjandrea May 26 '18 at 04:12
  • @wjandrea I'm having trouble making that work as an alias or bash function, like so: `alias chromemem="ps aux | grep -i \"chrome\" | awk '{vsz += $5; rss += $6} END { print \"vsz=\"vsz, \"rss=\"rss }'"` – Rushi Agrawal Aug 03 '19 at 08:35
  • 1
    @Rushi Don't bother. This solution doesn't give a meaningful number. Use `smem` per the top answer. Though FWIW, I would implement it as a script or function, since it's too complex for an alias. – wjandrea Aug 03 '19 at 14:41
  • Is this a number of _bytes_ ? It would be useful to add this info on the answer to save researching time fo the users who want to try this approach. – Kubuntuer82 Apr 15 '21 at 07:50
  • For example, if this is a number of bytes, I would add the following extra pipe at the end: `| numfmt --to iec --format "%8.2f"` – Kubuntuer82 Apr 15 '21 at 07:54
2

Running this:

perl -e '$a="x"x1000000000;sleep(10);print"done\n"'

takes up 1.8GB RAM. So you would expect running this:

perl -e '$a="x"x1000000000;fork;fork;fork;fork;sleep(10);print"done\n"'

would take up 16 times as much. But it does not.

This is due to the Linux kernel's intelligent copy-on-write: Because the contents of '$a' does not change, then the memory of '$a' can be shared. But it will only remain shared until '$a' is changed. When that happens, the changed section will be copied and start to take up RAM.

Whether you can measure how much memory is copy-on-write over-committed I do not know. But at least this explains your over-counting.

Ole Tange
  • 33,591
  • 31
  • 102
  • 198
1

Just quickly calculate the sum of the processes.

On Mac:

  • go to chrome://system/ and select all reported in mem_usage
  • paste in SublimeText
  • SelectAll (CMD+'A') and SelectAllLines (CMD+SHIFT+'L')
  • CMD+Right (go to eol), Backspace, Backspace, Backspace, ALT+Left, CMD+Backspace
  • Backspace, type '+', CMD+'A', CMD+'C'
  • open Terminal, run python, CMD+V, Enter

Et voila! "Easy"...

PS - Shortcut ninjas & 80s/90s Fighting-game players should have no problem with this solution

Kamafeather
  • 225
  • 1
  • 2
  • 8
0

I found a solution with vim and awk. Open chrome://system, expand mem_usage, copy to vim and execute the regexp:

:%s/\D*\(\d*\).MB.*/\1

This leaves only the numbers before MB. Save the file and execute

cat file | awk '{sum += $1} END {print sum}'

I could not convert the vim regexp code to sed.

0

There is USS which is available cross platforms

The USS (Unique Set Size) is the memory which is unique to a process and which would be freed if the process was terminated right now.

psutil>4.0 Python library can access it

Here is I would use it

sudo python3 -c "import psutil;print(sum(p.memory_full_info().uss for p in psutil.Process(pid=292).children())/1024/1024);"

where pid=292 is PID of most outer process from Activity Monitor

gadelat
  • 505
  • 1
  • 6
  • 15
-1

I knew that chrome/chromium had a task manager, but it doesn't give the total memory used. It turns out that the "Stats for nerds" link in the task manager leads to chrome://memory-redirect/ which does list the total memory used. It would be nice to have external validation of these numbers, as well as a way to get the information on the command line so more could be done with it, but this seems to be the best way available.

Ryan1729
  • 591
  • 1
  • 4
  • 7
  • 2
    This answer is not valid anymore. See https://bugs.chromium.org/p/chromium/issues/detail?id=588790 – eddygeek Nov 29 '16 at 12:57