Jump to content

Memory consumption of an executable


cuk
 Share

Recommended Posts

Hello. I am new to AutoIt scripting and I need an idea or some examples if possible to resolve next problem. So I need to make a script that check how much memory it consumes by six processes, all with the same name, say for example "wordpad.exe" Each wordpad.exe consumes different memory from system, the first consume 10 MB, second procces has consume 30 MB, etc. If any one from this six procces wordpad.exe consumption drops to less than 10 MB then I need to get a warning message let me know what process uses less memory. Is it possible? I apologize for my bad english language. Thanks

Link to comment
Share on other sites

Try this:

#AutoIt3Wrapper_UseX64=n
#include <EditConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

Local Const $Process_All_Access = 0x1F0FFF
Local $i, $ph, $PID, $kb, $mem, $sum, $iMemo, $max = 9
Local $ProcessHandle[$max + 1], $pid[$max + 1]
For $i = 0 To $max
    $pid[$i] = Run("Calc.exe")
    $ph = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $pid[$i])
    $ProcessHandle[$i] = $ph[0]
Next

Local $hGUI = GUICreate("Mem Usage (fast hack by UEZ 2010)", 400, 250, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS))
$iMemo = GUICtrlCreateEdit("", -1, -1, 400, 250, $ES_AUTOVSCROLL + $WS_VSCROLL)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($iMemo, 0xFFFFFF)
GUISetState()

While Sleep(1000)
    GUICtrlSetData($iMemo, "")
    For $i = 0 To $max
        $kb = Floor(_ProcessGetMem($ProcessHandle[$i]) / 1024)
        $sum += $kb
        GUICtrlSetData($iMemo, "Exe: Calc.exe, PID: " & $pid[$i] & ", mem usage: " & $kb & " kb" & @CRLF, 1)
    Next
    GUICtrlSetData($iMemo, "Total mem usage = " & $sum & " kb", 1)
    $sum = 0
WEnd

Func _Exit()
    GUIDelete($hGui)
    For $i = 0 To $max
        ProcessClose($pid[$i])
    Next
    Exit
EndFunc

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") / 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

Not tested on x64 os!

Br,

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Try this:

#AutoIt3Wrapper_UseX64=n
#include <EditConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

Local Const $Process_All_Access = 0x1F0FFF
Local $i, $ph, $PID, $kb, $mem, $sum, $iMemo, $max = 9
Local $ProcessHandle[$max + 1], $pid[$max + 1]
For $i = 0 To $max
    $pid[$i] = Run("Calc.exe")
    $ph = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $pid[$i])
    $ProcessHandle[$i] = $ph[0]
Next

Local $hGUI = GUICreate("Mem Usage (fast hack by UEZ 2010)", 400, 250, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS))
$iMemo = GUICtrlCreateEdit("", -1, -1, 400, 250, $ES_AUTOVSCROLL + $WS_VSCROLL)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($iMemo, 0xFFFFFF)
GUISetState()

While Sleep(1000)
    GUICtrlSetData($iMemo, "")
    For $i = 0 To $max
        $kb = Floor(_ProcessGetMem($ProcessHandle[$i]) / 1024)
        $sum += $kb
        GUICtrlSetData($iMemo, "Exe: Calc.exe, PID: " & $pid[$i] & ", mem usage: " & $kb & " kb" & @CRLF, 1)
    Next
    GUICtrlSetData($iMemo, "Total mem usage = " & $sum & " kb", 1)
    $sum = 0
WEnd

Func _Exit()
    GUIDelete($hGui)
    For $i = 0 To $max
        ProcessClose($pid[$i])
    Next
    Exit
EndFunc

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") / 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

Not tested on x64 os!

Br,

UEZ

Not tested, but a nice piece of code!
Link to comment
Share on other sites

Wow, thanks for your help. I do not understand all the script, but it works. I will study him and I hope to make the program what I need. I saw that starts more calc.exe and show memory consumption for every calc.exe process , but if one of them I close and start manual a new calc.exe process not indicate memory consumed by this new process. Anyway, thanks again for your help. I have a good starting point ;)

Edited by cuk
Link to comment
Share on other sites

Oh, I'm so noob, I fail to make the script to work properly. I need a help again. So should the script to monitor six processes "worpad.exe" which should not be started by the script and when one of the six process used memory drop below 10 MB then receive a MsgBox (0, 'process memory "," pid process memory Below 10 MB).

Link to comment
Share on other sites

What about this version?

#AutoIt3Wrapper_UseX64=n
#include <Array.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

Local Const $Process_All_Access = 0x1F0FFF
Local $i, $ph, $PID, $kb, $mem, $sum, $iMemo, $wordpad_pid
Local $ProcessHandle, $pname = "Wordpad.exe"
Local $hGUI = GUICreate("Mem Usage (fast hack by UEZ 2010)", 400, 250, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS))
$iMemo = GUICtrlCreateEdit("", -1, -1, 400, 250, $ES_AUTOVSCROLL + $WS_VSCROLL)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($iMemo, 0xFFFFFF)
GUISetState()

While Sleep(1000)
    GUICtrlSetData($iMemo, "")
    PID($pname)
    For $i = 1 To $wordpad_pid[0][0]
    $kb = Floor(_ProcessGetMem($ProcessHandle[$i]) / 1024)
    $sum += $kb
        If $kb < 10240 Then MsgBox (0, "Process Memory ", "PID " & $wordpad_pid[$i][1] & " process memory below 10 MB)")
    GUICtrlSetData($iMemo, "Exe: " & $pname & ", PID: " & $wordpad_pid[$i][1] & ", mem usage: " & $kb & " kb" & @CRLF, 1)
    Next
    GUICtrlSetData($iMemo, @CRLF & "Total mem usage = " & $sum & " kb", 1)
    $sum = 0
WEnd

Func PID($name = "Wordpad.exe")
    $wordpad_pid = ProcessList($name)
    Dim $ProcessHandle[$wordpad_pid[0][0] + 1]
    For $i = 1 To $wordpad_pid[0][0]
        $ph = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $wordpad_pid[$i][1])
        $ProcessHandle[$i] = $ph[0]
    Next
EndFunc

Func _Exit()
    GUIDelete($hGui)
    Exit
EndFunc

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") / 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

Br,

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...