8

I was comparing the memory usage of different portable browsers but this question is general for all programs that open up in several sub-processes in task manager.

See the screenshot from Sysinternals Process Explorer below:

One programm opens several processes

Firefox for example just opens up in one process, while Google Chrome starts several processes for each tab.

So how can I get the total RAM usage for all processes opened by one program? (Here for Google Chrome for example.)

Of course I could just use a calculator and reckon up the sum... but there must be a more convenient way!

  • 5
    This question is interesting because Chrome shares quite a lot of memory between these processes, so used memory is actually less than the sum. – Daniel B Mar 28 '15 at 20:34
  • 1
    The total private working set should give you a lower bound, and the total virtual size should give you an upper bound. It's probably somewhere between those two, maybe close to the total working set. – Mehrdad Mar 29 '15 at 5:40
  • No, the total virtual size is not an upper bound. That column reflects only the private virtual address space. It doesn't include shared mappings. The total virtual size would give an upper bound on the "Private working set", but not on the total RAM used. – Jamie Hanrahan Mar 28 '16 at 9:48
12

The general answer is: You can't, not accurately.

Assuming you're asking about RAM (physical memory) and not virtual - Windows publishes two relevant counters for each process, the total working set and the private (to the process) working set. (PerfMon will show you these.) Task Manager shows the "private" and "shared" (added together these would give the total). Process Explorer shows these as "Working Set" (the total), "WS Private", "WS Shareable", and "WS Shared". ProcExp does a little bit of "deep digging" to find the latter two.

The "shared", or the shared portion of the total, are what cause the problems.

Using the Process Explorer terms, "Shareable" shows RAM that has been allocated to the process but is at least potentially shared with other processes. And "Shared" is the subset of that actually is being shared, by at least one other process.

The trouble is that you don't know how many or which processes are actually sharing it. Adding up all of the "Shareable" or "Shared" numbers will give you a bigger total than reality, because shared pages will be counted in all of the processes that happen to be sharing them. Not every process, even processes running the same program (like Chrome), will necessarily be sharing the same pages out of their total address space, either.

For a set of processes all running the same .exe, like Chrome's: By adding together all of their "WS Private" counters, and then adding to that total the largest of any Chrome instance's "WS shareable", you will get to a minimum figure... but the actual may be larger.

Chrome does have that nice self-reporting feature mentioned by ge0rdi. The counters you want are the ones under "Memory". Chrome on my machine does not agree exactly with what the OS reports, but it's close enough for all practical uses.

Another thing to consider is that the "memory usage" of any process, or subset of processes, in a virtual memory OS is extremely dependent on the process's behavior and on RAM pressure. Run it on a system with more or less RAM, or with more or less other stuff running and demanding RAM, and the numbers will look very different. So there is not a fixed number as to how much RAM any given process "needs". So even "notice your available RAM, then close all of the Chrome instances and see how much available RAM increases" is not a good test. The OS may assign some of the released RAM to other processes, particularly if they're busy.

  • 1
    This is an interesting anwser, and the shared virtual memory thing is something i forgot... So if i understand you right, only the programm self knows how much memory it uses and windows actually can't tell you... therefore the programm has to offer an opportunity to output the usage, like Chrome has with chrome://memory-redirect – UdeF Mar 28 '15 at 21:47
  • Right. And the only reason Chrome can do that economically is that it knows how it's using those shared memory regions. The numbers it reports for other browsers are just the figures the OS offers. – Jamie Hanrahan Mar 28 '15 at 21:55
10

I don't know how to do this in general, but to measure total memory usage of Chrome you can write about://memory to address bar.

The Summary section shows total memory usage of all Chrome processes.

  • I'm not sure under windows, but it most definitely doesn't work under OSX. – o0'. Mar 29 '15 at 13:28
  • 1
    Alternatively you can press Shift + Esc to bring up the chrome task manager (similar to the windows task manager) and see stats (including memory) on every tab in chrome, chrome background process and chrome overall. – David says Reinstate Monica Mar 29 '15 at 16:51
  • no longer supported by chrome...so much for the 'nice feature' - guess they dont want anyone to know how much it hogs. FF honors this command. This is the one sole irritating factor that I want to move to FF. – killjoy May 6 '17 at 10:01
  • How to do it in OPERA ? – The Beast Aug 12 '17 at 17:15
0

A while back I wrote a great (and perhaps somewhat ugly) batch file that does it, it requires gnuwin32's sed and cut.

I wrote it 'cos I was horrified how much RAM modern browsers take up and I wanted to see quickly.

I am currently trying to neaten it up but this is it as it is.

So, it's saying that Chrome is using 3.4GB

    C:\>ramchrome<ENTER>
    >=1GB
    3GB
    3,480,288
    3.4GB (rounded down)
    3480288KB

Here is the batch file. At one time it was much simpler. But I wanted to add e.g. GB and KB .. and that takes up most of the batch file. It's very future proof because even if Chrome took up a bunch of yottabytes (a yottabyte is million trillion megabytes) then my batch should or at least, is written, to support that.

The basis of how the internals of the batch file works is simple.. It creates some temp files.. which you can look at.. You see it basically outputs a list of all the processes, filters out the ones for chrome.exe grabs just the part that states how many bytes it uses. Then the last temp file you can output you see it just has a list of all the bytes used by each chrome.exe process, with any commas or spaces removed, and then a for statement sums them all. The last temp file could be opened in excel.. though there isn't a need to.

You see how to use the bat file, just run it and see what it outputs. You don't need to know how it works, but that's how it works. You see from looking at the temp files it generates.

@echo off
setlocal enableextensions enabledelayedexpansion


set tempfile1=%temp%\asdf1.a
set tempfile2=%temp%\asdf2.a
set tempfile3=%temp%\asdf3.a
set tempfile4=%temp%\asdf4.xls

tasklist >%tempfile1%
type %tempfile1% | grep "chrome" > %tempfile2%
cut -b 68- %tempfile2% > %tempfile3%
sed -r "s/\d32|K|,//g" %tempfile3% >%tempfile4%


set total=0
for /f %%f in (%tempfile4%) do @(set /a total+=%%f)>nul

:: --- added this
set a=%total%

if "%a:~-9,-6%"=="" echo ^< 1GB
if NOT "%a:~-13,-6%"=="" echo ^>=1GB

set yotta=%a:~-24,-21%
set zetta=%a:~-21,-18%
set exa=%a:~-18,-15%
set peta=%a:~-15,-12%
set tera=%a:~-12,-9%
set giga=%a:~-9,-6%
set mega=%a:~-6,-3%
set kilo=%a:~-3%

set prefix=

if NOT "!yotta!"=="" (
    set yotta=!yotta!,
    if "!prefix!"=="" (
        set prefix=YB
        echo !a:~-24,-21!!prefix!
    )
)

if NOT "!zetta!"=="" (
    set zetta=!zetta!,
    if "!prefix!"=="" (
      set prefix=ZB
      echo !a:~-21,-18!!prefix!
    )
)

if NOT "!exa!"=="" (
    set exa=!exa!,
    if "!prefix!"=="" (
      set prefix=EB
      echo !a:~-18,-15!!prefix!
    )
)

if NOT "!peta!"=="" (
    set peta=!peta!,
    if "!prefix!"=="" (
      set prefix=PB
      echo !a:~-15,-12!!prefix!
    )
)

if NOT "!tera!"=="" (
    set tera=!tera!,
    if "!prefix!"=="" (
      set prefix=TB
      echo !a:~-12,-9!!prefix!
    )
)

if NOT "!giga!"==""  (
    set giga=!giga!,
    if "!prefix!"=="" (
      set prefix=GB
      echo !a:~-9,-6!!prefix!
    )
)

if NOT "!mega!"==""  (
    set mega=!mega!,
    if "!prefix!"=="" (
      set prefix=MB
      echo !a:~-6,-3!!prefix!
    )
)

if "!mega!"=="" (
  if "!prefix!"=="" (
   set prefix=KB
   echo !kilo!
  )
)


echo !yotta!!zetta!!exa!!peta!!tera!!giga!!mega!!kilo!

if "%prefix%"=="YB" echo !a:~-24,-21!.!a:~-21!YB (rounded down)
if "%prefix%"=="TB" echo !a:~-12,-9!.!a:~-9,1!TB  (rounded down)
if "%prefix%"=="GB" echo !a:~-9,-6!.!a:~-6,1!GB (rounded down)
if "%prefix%"=="MB" echo !a:~-6,-3!.!a:~-3,1!MB (rounded down)


:: echo %a:~-13,-6%,%a:~-6,-3%,%a:~-3%
 set a=
:: -----------

echo %total%KB
set tempfile1=
set tempfile2=
set tempfile3=
set tempfile4=

To summarize how it works, here is a sample of the first temp file asdf1.a as you can see it lists many processes not just chrome.exe

chrome.exe                    5572 Console                    1     64,784 K
chrome.exe                    3880 Console                    1    155,376 K
chrome.exe                   14516 Console                    1    106,932 K
chrome.exe                    2524 Console                    1     88,464 K
dllhost.exe                  15996 Console                    1      8,844 K
tasklist.exe                 16232 Console                    1      7,152 K

The second temp file gets closer to what we want, by filtering out chrome.exe

asdf2.a

chrome.exe                    5572 Console                    1     64,784 K
chrome.exe                    3880 Console                    1    155,376 K
chrome.exe                   14516 Console                    1    106,932 K
chrome.exe                    2524 Console                    1     88,464 K

asdf3.a gets closer still

 64,784 K
155,376 K
106,932 K
 88,464 K

asdf4.xml (don't ask why it's .xls maybe I intended to open it in excel)

64796
155592
114084
88464

So then you the batch file has generated a plain list to sum.

and obviously this line of the batch file does the summing

for /f %%f in (%tempfile4%) do @(set /a total+=%%f)>nul

Note Using a batch file to write this was silly.. Batch files are often not very portable because a slight change in the output of a command with a new version of the OS or a new version of a command, can make the program not work. And if it relies on third party programs where different versions of them are out there then it's not good. And many of gnuwin32's commands are out of date, cygwin tends to be more up to date with its versions of commands. This was running fine for me though now I tried it it gave an error.

I might write something in C# instead at some point.

Also note that as Rick pointed out, this doesn't take into account DLLs used by a program. Though personally I find what is useful to me is to know how much is used by the executable (or in the case of some modern browsers, the many instances of the executable!)

  • 1
    Sorry but this will not properly account for shared memory. This will result in a figure that is far too large. Even if you have just a single instance of a given executable name, because some of that "mem usage" is in system DLLs that are shred with every other process. I must, however, salute your .BAT skills. :) – Jamie Hanrahan Mar 28 '16 at 10:07
  • How to do it in OPERA ? – The Beast Aug 12 '17 at 19:30
  • @TheBeast well, doing it in a bat file was a bit crazy..bat files aren't that portable, a slight change of OS or version of a command can make them not work. so it was silly.. though was automated.. you could change grep "chrome" to grep opera but right now I get a bug with that bat, maybe a version of a command changed. But what you can do is see superuser.com/questions/209555/… So the command tasklist /FO csv > c:\crp\a.aaa then you can open it in excel, tell excel that comma is the delimiter... – barlop Aug 12 '17 at 21:44
  • you could then got to that column of data where it says the tasklist i.imgur.com/W8x47m9.png Then you could remove every K and comma from column D. And you could sort by Column A (process name). Then still using excel you could sum the total just for e.g. opera.exe. e.g. the sum from E12:E20 would be the sum for chrome.exe and if you had opera listed there at e.g. E90:E97 you could sum that. – barlop Aug 12 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.