Checking on process memory consumption on a Windows system

PureVPN
The Microsoft Windows tasklist command shows the memory consumed by running processes, but doesn't total the memory usage or provide a means to sort the process list by the amount of memory being used by a process. You can use a "memusage" filter, though, to see just those processes consuming above a threshold you specify in kilobytes (KB). E.g., if I only want to see processes consuming greater than 100,000 KB of memory, I could use the command below:

 
C:\>tasklist /fi "memusage gt 100000"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
csrss.exe                      600 Console                    1    267,148 K
svchost.exe                    752 Services                   0    101,284 K
soffice.bin                   4032 Console                    1    117,492 K
explorer.exe                 26968 Console                    1    131,316 K
thunderbird.exe              38256 Console                    1    213,356 K
Evernote.exe                 18624 Console                    1    105,492 K
chrome.exe                   35828 Console                    1    146,968 K
chrome.exe                   39512 Console                    1    107,996 K
chrome.exe                   41216 Console                    1    125,120 K
chrome.exe                   40656 Console                    1    151,180 K
chrome.exe                   37932 Console                    1    124,356 K
chrome.exe                   39072 Console                    1    119,300 K

If I want to total the amount of memory being used from the numbers displayed by the tasklist command, I can use a FOR /F loop and the findstr command. I can ignore the column header lines by specifying that I only want to look for lines ending with " K", e.g, if I want the total amount of memory used by all instances of PuTTY running on the system, I can look for putty.exe processes with the following:

 
C:\>tasklist /fi "imagename eq putty.exe" | findstr " K$
"
putty.exe                     3336 Console                    1      2,028 K
putty.exe                     6024 Console                    1      2,348 K
putty.exe                     7020 Console                    1      2,064 K
putty.exe                     7752 Console                    1      2,128 K
putty.exe                     8076 Console                    1      2,344 K
putty.exe                     5388 Console                    1      2,620 K
putty.exe                    24296 Console                    1      2,352 K
putty.exe                    21308 Console                    1      2,028 K
putty.exe                    22460 Console                    1      1,960 K
putty.exe                    27636 Console                    1      2,384 K
putty.exe                    28816 Console                    1      3,656 K
putty.exe                    29756 Console                    1      6,712 K
putty.exe                    31628 Console                    1      4,644 K
putty.exe                     2004 Console                    1     15,332 K

All I really want are the numbers in the 5th column, though. I can get those via a FOR /F loop. The individual items separated by spaces are "tokens" for the FOR /F loop.

 
C:\>for /f "tokens=5" %g in ('tasklist /fi "imagename eq putty.exe" ^| findstr " K$"') do @echo %g
2,028
2,348
2,064
2,128
2,344
2,620
2,352
2,028
1,960
2,384
3,656
6,712
4,644
15,300

The for loop assigns the output of the commands within the parentheses to the variable %g, but since I specified "tokens=5" only the numbers in the 5th column are assigned to the variable. The do @echo %g then displays each of those numbers with the @ before the echo, turning off the display of the command itself giving me only the number on each line as the loop is executed.

Next I need to add the numbers. That can be done using the set command. With the set command you can set a variable equal to some string of characters, e.g. set test="Hello world". If the /a option is used with the set command then the string to the right of the equal sign is treated as a number or numerical expression to be evaluated.

Generic Category (English)120x600
 
C:\>echo %test%
"hello world"

C:\>set /a test=2+5
7
C:\>echo %test%
7

But though commas between every 3 digits make numbers easier to understand for humans, they are not interpreted in the same way by the set command.

 
C:\>set /a test=10,000+11,000
0
C:\>echo %test%
10

So I need to remove any commas from numbers, which can be done via a batch file as shown below:

@echo off

setlocal EnableDelayedExpansion
set total=0

REM If no process name was entered on the command line, prompt for the process
REM name.
IF [%1]==[] (
   set /p pname="Process name: "
) ELSE (
   set pname=%1
)

for /f "tokens=5" %%i in ('tasklist /fi "imagename eq %pname%" ^| findstr " K$"
') do (
   set pmemuse=%%i
   REM eliminate the comma from the number
   set pmemuse=!pmemuse:,=!
   set /a total=!total! + !pmemuse!
)
echo %total% K

A process name can be provided on the command line, e.g., showmemusage putty.exe. If no process name is provided on the command line, the batch file will prompt for the name of the process for which a total should be provided and then add the memory usage for each process with that image name, displaying the total in kilobytes (KB).

By default for FOR loops, the entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script. So it is necessary to put setlocal EnableDelayedExpansion before the FOR loop and then put exclamation points around the variable names within the loop rather than the percent signs - see EnableDelayedExpansion for an explanation of this behavior for FOR loops.

The batch file will produce results as shown below:

C:\>showmemusage.bat putty.exe
52560 K

C:\>showmemusage.bat
Process name: putty.exe
52560 K

Note: the amount of memory consumed by processes can vary dynamically, so if you issued the tasklist /fi "imagename eq putty.exe" command and then immediately afterwards ran the batch job, the total might not be exactly the same from the batch job as what it was before, because one or more of the processes may later be consuming more or less memory that it was previously.

showmemusage.bat

References:

  1. In Windows cmd, how do I prompt for user input and use the result in another command?
    Date: August 3, 2009
    stackoverflow
  2. What is the proper way to test if variable is empty in a batch file, If NOT “%1” == “” GOTO SomeLabel, fails if %1 has quotes
    Date: March 29, 2010
    stackoverflow
  3. how to get substring of a token in for loop?
    Date: December 27, 2011
    stackoverflow
  4. Adding numbers containing commas in a batch script
    Date: July 25, 2011
    stackoverflow
  5. IF
    SS64 | Command line reference
  6. EnableDelayedExpansion
    SS64 | Command line reference

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px

Valid HTML 4.01 Transitional

Created: Tuesday, November 25, 2014