How do I get the raw number of bytes for a PID on Windows CLI?

178 views Asked by At

With lots of research, I've got to this point:

tasklist /fi "pid eq 13584" /fo CSV

Output:

"Image Name","PID","Session Name","Session#","Mem Usage"
"php.exe","13584","Console","1","25 660 K"

It is still an ugly mess. I'm trying to get output such as:

25660000

That is, no CSV or other "formatting"/unwanted data. No "formatted" amount of memory. Just raw bytes.

How is it done?

2

There are 2 answers

0
Anders On

Calling some external program and parsing its output is never going to be ideal, but here we go:

cmd.exe /D /V:ON /E:ON /c for /F "tokens=2 delims=K:" %a in ('tasklist /fi "pid eq 13584" /fo list^^^|find /i " K"') do @echo.%a

This almost works. You still have to remove the space and multiply by 1000, but doing that in PHP is easier than in batch code. It relies a bit too much on how tasklist outputs the data, and this is probably not something you should use in production.

But we can do better:

cmd.exe /D /V:OFF /E:ON /c for /F "skip=1 tokens=*" %a in ('wmic process where "processid=13584" get WorkingSetSize') do @echo.%a
0
Johan Bijker On

Look at wmic

wmic process where processid=13584 get WorkingSetSize