Search This Blog

Thursday, October 14, 2010

Memory Usage Using WMI

The below code can be used to get the memory usage, peak memory usage and the virtual memory usage as in the Task Manager without opening it. Here we are using the WMI object methods:

' To get the Machine Name

Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
For Each objItem in colItems
PCName = objItem.Name
Next

'To get the Memory Values

On Error Resume Next
arrComputers = Array(PCName)
For Each strComputer In arrComputers

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
For Each objItem In colItems

If objItem.Name = "ProcessNameYouWant.exe" Then
VM = objItem.PageFileUsage/1024
End If
Next

Set MemItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In MemItems

If objItem.Name = "ProcessNameYouWant" Then
mem = objItem.WorkingSet/1024
peak = objItem.WorkingSetPeak/1024
End If
Next
Next

1 comment:

  1. If we run this script from QTP it is not capturing value in mem and peak variables. Do we have to initiate wbemFlagReturnImmediately + wbemFlagForwardOnly variables with Hexa in QTP. What will be the values?

    ReplyDelete