9

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!

Mat
  • 8,353
UdeF
  • 315
  • 1
  • 3
  • 8

3 Answers3

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.

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.

ge0rdi
  • 1,591
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!)

barlop
  • 25,198