Logging a process from the task manager

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Carnivean
Newbie
Posts: 17
Joined: Fri Jun 10, 2011 8:04 am

Logging a process from the task manager

Post by Carnivean » Fri Jun 24, 2011 7:02 am

Hi

I wonder if someone can point me in the right direction here.

I'm testing an application which has a memory leak, at periods throughout the night I want to open the task manager and record the process details for this application. I've written a script which highlights the process and uses 'GetTextInRect' to capture the process details into a text file. This works sometimes however what I find is that my applications process doesn't always appear in the same place.

I've also used the Image recognition feature to do the same thing but this seems a bit over kill for this.

I haven't written any code before and I'm still just learning with this particular tool so any answers please keep them simple.

I've attached my script.

//Gets the list of processes for system status.exe from task manager and saves to a text file
SRT>Task Manager
Let>RP_WAIT=2
Run>taskmgr.exe
WaitWindowOpen>Windows Task Manager
WindowAction>1,Windows Task Manager
Wait>1.2
Send>system status.exe
SetFocus>Windows Task Manager
Wait>1
GetTextInRect>24,110,840,125,Taskmanager
TimeStamp>C:\Documents and Settings\Administrator\Desktop\Script Logging\Target Keypad Stress Test.txt,%Taskmanager%
Wait>0.5
CloseWindow>Windows Task Manager
End>Task Manager

Thanks for any help

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Jun 24, 2011 8:49 am

Well, I would scrap that method entirely, as you can get memory usage of a process from the command line using the tasklist command.

So this script uses that to get the memory usage of a given process:

Code: Select all

//get memory usage of a process:
Let>process=outlook.exe

Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>cmd.exe /c tasklist /FI "IMAGENAME eq %process%" >> "%TEMP_DIR%memusage.txt"
ReadFile>%TEMP_DIR%\memusage.txt,theInfo
DeleteFile>%TEMP_DIR%\memusage.txt

//the last value contains the memory usage
Let>pattern=\s[\d,]+\sK$
RegEx>pattern,theInfo,0,matches,nm,0
Let>mem_usage=matches_1

//so we could now log mem_usage to file or whatever you want to do with it
MessageModal>Memory Usage of %process%: %mem_usage%
Adapt to suit your requirements/config.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Jun 24, 2011 8:55 am

Or you could do:

Code: Select all

VBSTART
Function GetProcessMemoryUsage(procname)
  Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  Set colObjects = objWMI.ExecQuery("Select * From Win32_Process")
  For Each Item in colObjects
    If Item.Name=procname Then
      GetProcessMemoryUsage = Item.WorkingSetSize
      Exit For
    End if
  Next
End Function
VBEND

VBEVal>GetProcessMemoryUsage("OUTLOOK.EXE"),size
MessageModal>size
Although I think that's a slightly different metric. Values available to you can be seen here:
http://msdn.microsoft.com/en-us/library ... s.85).aspx
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Carnivean
Newbie
Posts: 17
Joined: Fri Jun 10, 2011 8:04 am

Post by Carnivean » Fri Jun 24, 2011 9:23 am

Thanks for the prompt reply Marcus.

As yet I don't fully understand the middle section '//the last value contains the memory usage' but it works great retrieving the memory usage.

I've now looked under the tasklist but can't see if there is a way of recalling other parameters that are listed in the Task manager such as 'VM Size' I'm interested in logging this aswell as the Memory usage?

Thanks

User avatar
Marcus Tettmar
Site Admin
Posts: 7391
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Jun 24, 2011 9:24 am

Yes, in exactly the same way.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts