shashij
shashij

Reputation: 83

Memory utilisation of a process

I'm running this script in Windows 7, but I'm not getting right memory used by a process:

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colObjects = objWMI.ExecQuery("Select * From Win32_Process where Caption='firefox.exe'")

For Each Item in colObjects
    WScript.Echo Item.Name & " - " & Item.WorkingSetSize
Next

I m getting the values in minus something like this -289210368

Can anyone help me to get the right memory used in megebytes (MB)?

Upvotes: 2

Views: 1766

Answers (1)

Hackoo
Hackoo

Reputation: 18837

Try using this function : Function ConvertSize(Size) like this :

Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colObjects = objWMI.ExecQuery("Select * From Win32_Process where Caption='firefox.exe'")

For Each Item in colObjects
    WScript.Echo Item.Name & " = " & ConvertSize(Item.WorkingSetSize)
Next
'******************************************************************************************
Function ConvertSize(Size) 
    Do While InStr(Size,",") 'Remove commas from size     
        CommaLocate = InStr(Size,",")     
        Size = Mid(Size,1,CommaLocate - 1) & _         
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
    Loop
    Dim Suffix:Suffix = " Bytes" 
    If Size >= 1024 Then suffix = " KB" 
    If Size >= 1048576 Then suffix = " MB" 
    If Size >= 1073741824 Then suffix = " GB" 
    If Size >= 1099511627776 Then suffix = " TB" 
    Select Case Suffix    
    Case " KB" Size = Round(Size / 1024, 1)     
    Case " MB" Size = Round(Size / 1048576, 1)     
    Case " GB" Size = Round(Size / 1073741824, 1)     
    Case " TB" Size = Round(Size / 1099511627776, 1) 
    End Select
    ConvertSize = Size & Suffix 
End Function
'******************************************************************************************

You can also if you want of course to put some processes into array and check their size like this :

Option Explicit
Dim Title,ProcessArray,Process,Msg 
Title = "PROCESS SIZE by Hackoo 2015"
ProcessArray = Array("explorer.exe","firefox.exe","chrome.exe","iexplore.exe","Opera.exe",_
"Skype.exe","IDMan.exe","CCleaner.exe","svchost.exe","winlogon.exe","VLC.exe","wscript.exe","WINWORD.exe")  
    For Each Process In ProcessArray     
      Msg = Msg & Process & " = " & Process_Size(Process) & VbCrlF  
    Next  
MsgBox Msg,VbInformation,Title
'****************************************************************************************** 
Function Process_Size(ProcessName)  
Dim objWMI,colObjects,Item
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set colObjects = objWMI.ExecQuery("Select * From Win32_Process where Caption= '"& ProcessName & "'")
For Each Item in colObjects
   Process_Size = ConvertSize(Item.WorkingSetSize)
Next
End Function
'******************************************************************************************
Function ConvertSize(Size) 
    Dim CommaLocate
    Do While InStr(Size,",") 'Remove commas from size     
        CommaLocate = InStr(Size,",")     
        Size = Mid(Size,1,CommaLocate - 1) & _         
        Mid(Size,CommaLocate + 1,Len(Size) - CommaLocate) 
    Loop
    Dim Suffix:Suffix = " Bytes" 
    If Size >= 1024 Then suffix = " KB" 
    If Size >= 1048576 Then suffix = " MB" 
    If Size >= 1073741824 Then suffix = " GB" 
    If Size >= 1099511627776 Then suffix = " TB" 
    Select Case Suffix    
    Case " KB" Size = Round(Size / 1024, 1)     
    Case " MB" Size = Round(Size / 1048576, 1)     
    Case " GB" Size = Round(Size / 1073741824, 1)     
    Case " TB" Size = Round(Size / 1099511627776, 1) 
    End Select
    ConvertSize = Size & Suffix 
End Function
'******************************************************************************************

Upvotes: 2

Related Questions