Retrieve memory consumption of a process

2.2k Views Asked by At

My AutoIt script automates test cases. I suspect something is leaking memory. It starts at 10 MB, when test cases are over it reaches around 40 MB (Task Manager values).

I want my AutoIt script to report memory consumption after each test case. Knowing the difference I probably can locate the cause.

How can I retrieve memory consumption of a process using an AutoIt script?

1

There are 1 best solutions below

2
On BEST ANSWER

You are looking for the WorkingSetSize, which is probably not the exact same value like the one listed in your task manager. According to it's documentation, the working set is the amount of memory physically mapped to the process context at a given time.

Here's how to determine it:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$pid = Run("notepad")

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE ProcessId = " & $pid, "WQL", _
                $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) And $pid <> -1 Then
   For $objItem In $colItems
      ConsoleWrite("WorkingSetSize: " & $objItem.WorkingSetSize & @CRLF)
   Next
Else
   ConsoleWrite("No WMI Objects Found for class 'Win32_Process' with ProcessId = " & $pid & @CRLF)
EndIf

You can as well find out any other value from the Win32_Process object with this method.