Based upon the advice I provided in my comment, and using a different method of determining the process associated with the PID, you could do it like this, (change the PID, 5716
, on line 2
as necessary):
@SetLocal EnableExtensions
@Set "PID=5716"
@Set "$Mem="
@For /F "Tokens=2 EOL=N Delims=," %%G In (
'%__AppDir__%wbem\WMIC.exe OS Get TotalVirtualMemorySize^,Version^
/Format:CSV 2^>NUL')Do @For /F "Tokens=2 EOL=N Delims=," %%H In (
'%__AppDir__%wbem\WMIC.exe Path Win32_PerfFormattedData_PerfProc_Process^
Where "IDProcess='%PID%'" Get WorkingSet^,WorkingSetPrivate /Format:CSV^
2^>NUL')Do @Set /A "$Mem=%%H/1024*100/%%G">NUL 2>&1
@If Defined $Mem Echo %$Mem%%%&Pause
Please note however, that if the arithmetic meets numbers which exceed the Set /A
limitation of 32-bit signed integers, the method will fail. Also, you should note that because we're working with integers, the result will always be rounded down to the nearest integer. This means anything below 1%
will show as 0%
If I remember correctly, there is a delay when you use this method for the first time in any session, after that, you'll usually notice a significant speed increase.
If you already know the name of the process, which is usually the name of the executable without its extension, then you could use that instead of the PID,
(change the ExecutableFile
string on line 2
to your process name as necessary):
@SetLocal EnableExtensions
@Set "MyProcess=ExecutableFile"
@Set "$Mem="
@For /F "Tokens=2 EOL=N Delims=," %%G In (
'%__AppDir__%wbem\WMIC.exe OS Get TotalVirtualMemorySize^,Version^
/Format:CSV 2^>NUL')Do @For /F "Tokens=2 EOL=N Delims=," %%H In (
'%__AppDir__%wbem\WMIC.exe Path Win32_PerfFormattedData_PerfProc_Process^
Where "Name='%MyProcess%'" Get WorkingSet^,WorkingSetPrivate /Format:CSV^
2^>NUL')Do @Set /A "$Mem=%%H/1024*100/%%G">NUL 2>&1
@If Defined $Mem Echo %$Mem%%%&Pause