Sign in to follow this  
Followers 0
chipDE

Process Memory use Win 7

16 posts in this topic

Hi,

i have a problem. I want to read how much memory a process in Win 7 use. Windows 7 not used WorkingSetSize but PrivateWorkingSetSize. I found a tutorial in C for the calculation of PrivateWorkinSetSize: http://processlasso.blogspot.com/2009/09/calculating-private-working-set-size.html

Can someone convert me to autoit?

Share this post


Link to post
Share on other sites



Thank you, but how can I use for a particular process?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thank you, but how can I use for a particular process?

Hi,

for one existing process.

If you have more then one process to check, this should be a good start to code with.

$proc = "iexplore.exe"
MsgBox (0,"", "Prozess " & $proc & " occupies " & _getprivbytes (ProcessExists ($proc)) / 1024 & " kb RAM")

Func _getprivbytes ($pid)
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
       For $objItem In $colItems
          If $pid = $objItem.IDProcess Then Return $objItem.PrivateBytes
       Next
    Endif
    Return 0
EndFunc

You should download scriptomatic.au3 so you can produce WMI Code by yourself.....

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

Thanks,

but not working properly. I get the data (Win 7 x64 and Win 7 x86) for the process "outlook.exe" and become "Prozess outlook.exe occupies 53908 kb RAM" but in the Windows Taskmanager the Ram are 6536. This is a different of approximately 47000 kb.

Share this post


Link to post
Share on other sites

Thanks,

but not working properly. I get the data (Win 7 x64 and Win 7 x86) for the process "outlook.exe" and become "Prozess outlook.exe occupies 53908 kb RAM" but in the Windows Taskmanager the Ram are 6536. This is a different of approximately 47000 kb.

Hi,

sorted out, that is should be the WorkingSetPrivate option. Tested on Win7 64Bit and running well.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
$proc = "iexplore.exe"
MsgBox (0,"", "Prozess " & $proc & " occupies " & _getprivbytes (ProcessExists ($proc)) / 1024 & " kb RAM")

Func _getprivbytes ($pid)
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $strComputer = "localhost"

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) then
       For $objItem In $colItems
          If $pid = $objItem.IDProcess Then Return $objItem.WorkingSetPrivate
       Next
    Endif
    Return 0
EndFunc

;-))

Stefan

Share this post


Link to post
Share on other sites

Yes, this is it. Very very very thank you. :mellow:

Share this post


Link to post
Share on other sites

Is that only available in Win7 or only in x64, or both? I went here: MSDN: Win32_PerFormattedData_PerfProc_Process and the .WorkingSetPrivate property isn't listed.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hi,

strange thing. I haven't had the wmi class on my xp sp3 prof 32 Bit home computer, but at work.

So, i was at home, i compiled scritpomatic.au3 to 64 bit exe and run it on Win7 Enterprise 64 Bit vm machine.

Found WMI class (huuh) and then a little try and error since i found the appropriate object.

As i have Autoit only installed on my xp, i have to jumped from Win 7 to xp and code the WMI code, compile it and run it on my vm Win 7 machine for testing.

So i don't know, why i haven't the class on my xp machine and if this is a Win 7 64 bit feature either.

Maybe somone could try and post answer.

;-))

Stefan

@Edit:

Compiled it with 32 bit and test on my XP SP 3 32 bit: 0 kb RAM for a cardgame.exe

Same on WIn 7 64 Bit 23.028 KB

So it might be a Win7 and / or Vista feature.

As i have this WMI class not on my machine, i couldn't even say, if you have this option on XP machine.

Edited by 99ojo

Share this post


Link to post
Share on other sites

Maybe somebody can help to get the right solution:

#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Change2CUI=y
Global $PID
Global Const $Process_All_Access = 0x1F0FFF

$PID = ProcessExists(@AutoItPID)
If $PID = 0 Then
    ConsoleWrite(@CRLF & "ERROR! Process " & $PID & " not found! Aborting..." & @CRLF)
    Exit
EndIf

$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

MsgBox(0, "PrivateUsage Size", Round(_ProcessGetMem($ProcHandle) / 1024, 0) & " kb")

DllCall("psapi.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
Exit

Func _ProcessGetMem($ProcessHandle) ;get physical memory of the process -> http://msdn.microsoft.com/en-us/library/ms683219%28VS.85%29.aspx
    Local $structPROCESS_MEMORY_COUNTERS, $structPROCESS_MEMORY_COUNTERS_EX, $nSize, $aRet
    If @OSVersion <> "WIN_7" Then
        $structPROCESS_MEMORY_COUNTERS = DllStructCreate("dword cb; dword PageFaultCount; uint PeakWorkingSetSize; uint WorkingSetSize; " & _
                                                         "uint QuotaPeakPagedPoolUsage; uint QuotaPagedPoolUsage; uint QuotaPeakNonPagedPoolUsage; " & _
                                                         "uint QuotaNonPagePoolUsage; uint PagefileUsage; uint PeakPagefileUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
        $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS)
        $aRet = DllCall("psapi.dll", "int", "GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS), "dword", $nSize) ;call GetProcessMemoryInfo
        If $aRet[0] = 0 Then
            ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
            SetError(1, 0, $aRet[0])
        EndIf
        Return DllStructGetData($structPROCESS_MEMORY_COUNTERS, "WorkingSetSize")
    Else
        $structPROCESS_MEMORY_COUNTERS_EX = DllStructCreate("dword cb; dword PageFaultCount; uint PeakWorkingSetSize; uint WorkingSetSize; " & _
                                                            "uint QuotaPeakPagedPoolUsage; uint QuotaPagedPoolUsage; uint QuotaPeakNonPagedPoolUsage; " & _
                                                            "uint QuotaNonPagePoolUsage; uint PagefileUsage; uint PeakPagefileUsage; " & _
                                                            "uint PrivateUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
        $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS_EX)
        $aRet = DllCall("Kernel32.dll", "int", "K32GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS_EX), "dword", $nSize) ;call GetProcessMemoryInfo
        If $aRet[0] = 0 Then
            ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
            SetError(1, 0, $aRet[0])
        EndIf
        ConsoleWrite("WorkingSetSize: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "WorkingSetSize") / 1024, 0) & @CRLF & _
                     "PrivateUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage") / 1024, 0) & @CRLF & @CRLF)
        Return DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage")
    EndIf
EndFunc ;==>_ProcessGetMem

Thanks,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hi,

as i haven't toggle around with dll calls, you could try WMI class mentioned by Psalty.

I attach the version, i get the solution for chip.

But from my forum visitings, i think, maybe Psalty has a solution for the dll call.

;-))

Stefan

@Edit: Für Rückfragen....

scriptomatic.au3

Edited by 99ojo

Share this post


Link to post
Share on other sites

Hi,

as i haven't toggle around with dll calls, you could try WMI class mentioned by Psalty.

I attach the version, i get the solution for chip.

But from my forum visitings, i think, maybe Psalty has a solution for the dll call.

;-))

Stefan

@Edit: Für Rückfragen....

scriptomatic.au3

The issue is not WMI but the DLL call with Win7 and probably with Win2k8 R2! Because when you try to read every 500ms the memory WMI solution will probably too slow!

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hahaha,

indeed, i didn't know.

But it is sometimes a good way if you are not in hustle....

;-))

Stefan

@Edit: But, for christ sake, where is the WMI option WorkingSetPrivate coming from, as it isn't mentioned in msdn.

@Edit2:

If this is solved it's hopefully posted. I'm interested about the solution.

BTW: Why to check every 500 ms memory?

Edited by 99ojo

Share this post


Link to post
Share on other sites

Hahaha,

indeed, i didn't know.

But it is sometimes a good way if you are not in hustle....

;-))

Stefan

@Edit: But, for christ sake, where is the WMI option WorkingSetPrivate coming from, as it isn't mentioned in msdn.

@Edit2:

If this is solved it's hopefully posted. I'm interested about the solution.

BTW: Why to check every 500 ms memory?

If you want to write something like a process explorer or for any other reason to limit memory usage you need to check it periodically! I'm using WMI mostly for remote enumerations!

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

UEZ I don't think that should work with x64 AutoIt. PROCESS_MEMORY_COUNTERS is:

typedef struct _PROCESS_MEMORY_COUNTERS {
  DWORD  cb;
  DWORD  PageFaultCount;
  SIZE_T PeakWorkingSetSize;
  SIZE_T WorkingSetSize;
  SIZE_T QuotaPeakPagedPoolUsage;
  SIZE_T QuotaPagedPoolUsage;
  SIZE_T QuotaPeakNonPagedPoolUsage;
  SIZE_T QuotaNonPagedPoolUsage;
  SIZE_T PagefileUsage;
  SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS, *PPROCESS_MEMORY_COUNTERS;
In AutoIt that's something like:
DllStructCreate("dword cb;" & _
        "dword PageFaultCount;" & _
        "dword_ptr PeakWorkingSetSize;" & _
        "dword_ptr WorkingSetSize; " & _
        "dword_ptr QuotaPeakPagedPoolUsage;" & _
        "dword_ptr QuotaPagedPoolUsage;" & _
        "dword_ptr QuotaPeakNonPagedPoolUsage; " & _
        "dword_ptr QuotaNonPagePoolUsage;" & _
        "dword_ptr PagefileUsage;" & _
        "dword_ptr PeakPagefileUsage")


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Indeed, the problem was the struct to get also data from x64:

#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
Global $PID
Global Const $Process_All_Access = 0x1F0FFF

$PID = ProcessExists(@AutoItPID)
If $PID = 0 Then
    ConsoleWrite(@CRLF & "ERROR! Process " & $PID & " not found! Aborting..." & @CRLF)
    Exit
EndIf

$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

MsgBox(0, "PrivateUsage Size", Round(_ProcessGetMem($ProcHandle) / 1024, 0) & " kb")

DllCall("psapi.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
DllCall("kernel32.dll", "int", "CloseHandle", "int", $ProcHandle)
Exit

Func _ProcessGetMem($ProcessHandle) ;get physical memory of the process -> http://msdn.microsoft.com/en-us/library/ms683219%28VS.85%29.aspx
    Local $structPROCESS_MEMORY_COUNTERS, $structPROCESS_MEMORY_COUNTERS_EX, $nSize, $aRet
    If @OSBuild < 7600 Then
    $structPROCESS_MEMORY_COUNTERS = DllStructCreate("dword cb; dword PageFaultCount; dword_ptr PeakWorkingSetSize; ulong_ptr WorkingSetSize; " & _
    "dword_ptr QuotaPeakPagedPoolUsage; dword_ptr QuotaPagedPoolUsage; dword_ptr QuotaPeakNonPagedPoolUsage; " & _
    "dword_ptr QuotaNonPagePoolUsage; dword_ptr PagefileUsage; dword_ptr PeakPagefileUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
    $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS)
    $aRet = DllCall("psapi.dll", "int", "GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS), "dword", $nSize) ;call GetProcessMemoryInfo
    If $aRet[0] = 0 Then
    ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
    SetError(1, 0, $aRet[0])
    EndIf
    Return DllStructGetData($structPROCESS_MEMORY_COUNTERS, "WorkingSetSize")
    Else
    $structPROCESS_MEMORY_COUNTERS_EX = DllStructCreate("dword cb; dword PageFaultCount; dword_ptr PeakWorkingSetSize; dword_ptr WorkingSetSize; " & _
    "dword_ptr QuotaPeakPagedPoolUsage; dword_ptr QuotaPagedPoolUsage; dword_ptr QuotaPeakNonPagedPoolUsage; " & _
    "dword_ptr QuotaNonPagePoolUsage; dword_ptr PagefileUsage; dword_ptr PeakPagefileUsage; " & _
    "dword_ptr PrivateUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
    $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS_EX)
    $aRet = DllCall("Kernel32.dll", "int", "K32GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS_EX), "dword", $nSize) ;call GetProcessMemoryInfo
    If $aRet[0] = 0 Then
    ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
    SetError(1, 0, $aRet[0])
    EndIf
    ConsoleWrite("WorkingSetSize: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "WorkingSetSize"), 0) & @CRLF & _
    "PageFaultCount: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PageFaultCount"), 0) & @CRLF & _
    "PeakWorkingSetSize: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PeakWorkingSetSize"), 0) & @CRLF & _
    "QuotaPeakPagedPoolUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "QuotaPeakPagedPoolUsage"), 0) & @CRLF & _
    "QuotaPagedPoolUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "QuotaPagedPoolUsage"), 0) & @CRLF & _
    "QuotaPeakNonPagedPoolUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "QuotaPeakNonPagedPoolUsage"), 0) & @CRLF & _
    "QuotaNonPagePoolUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "QuotaNonPagePoolUsage"), 0) & @CRLF & _
    "PagefileUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PagefileUsage"), 0) & @CRLF & _
    "PeakPagefileUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PeakPagefileUsage"), 0) & @CRLF & _
    "PrivateUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage"), 0) & @CRLF & @CRLF)
    Return DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage")
    EndIf
EndFunc ;==>_ProcessGetMem

But the return value of PrivateUsage is different than shown in taskmanager (which is generally lower)!

Maybe PrivateUsage here is a different value than from taskmanager!

When I compare it with Process Explorer I can see following difference!

Test.exe

WorkingSetSize: 5680

PrivateUsage: 2104

From Process Explorer:

Working Set: 7336

WS Private: 1712

WS Shareable: 5620

WS Shared: 4812

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0