Obtaining memory usage of a process and string manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
xga
Posts: 6
Joined: 03 Jan 2012 20:13

Obtaining memory usage of a process and string manipulation

#1 Post by xga » 29 Jun 2012 09:30

Hi Guys,

I'm trying to write a dos batch file to monitor the memory usage of a certain process and once it reaches a certain threshold, restart the process.

The code below reports the memory usage of process.exe in kilobytes. E.g. 100,000

Code: Select all

for /f "tokens=5,6 delims=: " %%A in ('tasklist /nh /fi "imagename eq process.exe"') do echo %%A


So my question is, how do I convert that string result to an integer so that I can perform a "if gtr than" process? Do I need to strip the comma from the string first?

Any help would be greatly appreciated!

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Obtaining memory usage of a process and string manipulat

#2 Post by foxidrive » 29 Jun 2012 10:23

CMD math is limited to 2^31 or around 2.1 gb

Is that going to be a problem?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Obtaining memory usage of a process and string manipulat

#3 Post by foxidrive » 29 Jun 2012 10:32

I see that it reports in K without bytes as you said.

This sort of thing should work - I don't see the need for the delims in my window in Win7.
You might need the /f switch in taskkill
(Untested)

Code: Select all

@echo off
:loop
set "task=process.exe"
for /f "tokens=5,6 delims=, " %%A in ('tasklist /nh /fi "imagename eq %task%"') do (
if %%A%%B GTR 999000 do (
taskkill /im "%task%"
start "" "%task%"
)
)
ping -n 60 localhost >nul
goto :loop



EDIt: added delims
Last edited by foxidrive on 29 Jun 2012 20:27, edited 1 time in total.

xga
Posts: 6
Joined: 03 Jan 2012 20:13

Re: Obtaining memory usage of a process and string manipulat

#4 Post by xga » 29 Jun 2012 18:39

CMD math is limited to 2^31 or around 2.1 gb

Is that going to be a problem?


No, I'm only wanting to monitor the memory up to 1.5 GB or 1,500,000 KB.

xga
Posts: 6
Joined: 03 Jan 2012 20:13

Re: Obtaining memory usage of a process and string manipulat

#5 Post by xga » 29 Jun 2012 19:03

I see that it reports in K without bytes as you said.

This sort of thing should work - I don't see the need for the delims in my window in Win7.
You might need the /f switch in taskkill
(Untested)

Code:
@echo off
:loop
set "task=process.exe"
for /f "tokens=5,6" %%A in ('tasklist /nh /fi "imagename eq %task%"') do (
if %%A%%B GTR 999000 do (
taskkill /im "%task%"
start "" "%task%"
)
)
ping -n 60 localhost >nul
goto :loop


Thanks foxidrive. However this still will not work as if the memory usage was at 500 MB, tasklist reports it as "500,000" including the comma. As far as I am aware, the "IF" command cannot perform comparisons with values when they have a comma in them, so that's why I was thinking that I would have to somehow strip the comma from the value first.

Any thoughts?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Obtaining memory usage of a process and string manipulat

#6 Post by foxidrive » 29 Jun 2012 20:29

Silly me. I added the delims to the code above - try that.

It should present the number without a comma - but I don't know what the tasklist outputs when the command uses more than 1 GB of RAM.

Post Reply