Track memory usage in QTP with WMI

I used QTP to simulate a repeating user scenario in order to trace a memory leak with YourKit over time. So I’m using this quick & dirty function to poll the application regularly about it’s raw memory usage.


Function GetMemory
	pid = GetWindow().GetROProperty("process id")
	Set wmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
	Set resultset = wmiService.ExecQuery("SELECT WorkingSetSize FROM Win32_Process WHERE Handle=" & pid)
	For Each result In resultset
		GetMemory = Int(result.WorkingSetSize)
	Next
End Function

GetWindow() returns a reference to the application window.

It seems even though I SELECT only WorkingSetSize I could get other information such as Handle from the result.

The For Each loop was an easy way out since I didn’t know how to access the first element.

Reference: http://www.learnqtp.com/windows-management-instrumentation-wmi-qtp/