Jump to content

RAM monitor in System tray


jerome
 Share

Recommended Posts

Hello,

A Ram monitorwith an optimization option in system tray!

JD

#include <GuiConstants.au3>

RAMRun()

Func RAMRun()
 RAMInit()
 TrayCreateItem("Exit")
 TrayItemSetOnEvent(-1, "RAMExit") 
 TraySetState()
 While 1
  Sleep(50)
    WEnd
EndFunc

Func RAMInit()
 Opt("RunErrorsFatal", 0)
 Opt("GUIOnEventMode", 1)
 Opt("TrayOnEventMode",1)
 Opt("TrayMenuMode",   1)  ; Default tray menu items (Script Paused/Exit) will not be shown.
 Opt("GUIResizeMode",  $GUI_DOCKSIZE+$GUI_DOCKLEFT)
 TrayCreateItem("Memory audit")
 TrayItemSetOnEvent(-1, "RAMmonitor")
EndFunc

Func RAMmonitor()
 Opt("GUIOnEventMode", 0)
 $gui = GuiCreate("Memory monitor", 544, 190,-1, -1 , $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS)
 $Label_1   = GuiCtrlCreateLabel("Physical RAM:", 10, 10, 130, 20, )
 GUICtrlSetBkColor($Label_1, $GUI_BKCOLOR_TRANSPARENT)
 $Progress_1 = GuiCtrlCreateProgress(10, 30, 520, 20)
 $Label_2   = GuiCtrlCreateLabel("Pagefile size:", 10, 60, 130, 20)
 GUICtrlSetBkColor($Label_2, $GUI_BKCOLOR_TRANSPARENT)
 $Progress_2 = GuiCtrlCreateProgress(10, 80, 520, 20)
 $Label_3   = GuiCtrlCreateLabel("Virtual RAM size:", 10, 110, 130, 20)
 GUICtrlSetBkColor($Label_3, $GUI_BKCOLOR_TRANSPARENT)
 $Progress_3 = GuiCtrlCreateProgress(10, 130, 520, 20)
    $Button  = GUICtrlCreateButton("Optimize", 480, 160, 60, 25)
 GuiSetState()
 $nb=500
 While 1
  $nb=$nb+10
  If $nb > 500 Then
   $memory = MemGetStats ( )
   $RamFree = Round(100*($memory[1]-$memory[2])/$memory[1], 1)
   GUICtrlSetData ($Progress_1, $RamFree)
   GUICtrlSetData ($Label_1, "Physical RAM: " & $RamFree & "%")
   $RamFree = Round(100*($memory[3]-$memory[4])/$memory[3], 1)
   GUICtrlSetData ($Progress_2, $RamFree)
   GUICtrlSetData ($Label_2, "Pagefile size: " & $RamFree & "%")
   $RamFree = Round(100*($memory[5]-$memory[6])/$memory[5], 1)
   GUICtrlSetData ($Progress_3, $RamFree)
   GUICtrlSetData ($Label_3, "Virtual RAM size: " & $RamFree & "%")
   $nb=0
  EndIf
  Sleep(10)
  $msg = GuiGetMsg()
  If $msg = $GUI_EVENT_CLOSE Then ExitLoop
  If $msg = $Button Then MemoryOptimize()
 WEnd
 GUIDelete($gui)
 Opt("GUIOnEventMode", 1)
EndFunc

Func RAMExit()
 Exit
EndFunc

;---------------------------------------------------------------
;- Reduce memory
;---------------------------------------------------------------
Func MemoryOptimize()
 $memory1 = MemGetStats ( )
 Do
  ReduceMemory()
  $list = ProcessList()
  For $i=1 To $list[0][0]
   ReduceMemory($list[$i][1])
  Next
  $memory2 = $memory1
  $memory1 = MemGetStats ( )
 Until $memory1[2] >= $memory2[2]
EndFunc
 
Func ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc;==> ReduceMemory()
Edited by jerome
Link to comment
Share on other sites

Yes, you are right, it's better.

I Correct it:

#include <GuiConstants.au3>

RAMRun()

Func RAMRun()
    RAMInit()
    TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1, "RAMExit")   
    TraySetState()
    While 1
     Sleep(50)
    WEnd
EndFunc

Func RAMInit()
    Opt("RunErrorsFatal", 0)
    Opt("GUIOnEventMode", 1)
    Opt("TrayOnEventMode",1)
    Opt("TrayMenuMode",   1)  ; Default tray menu items (Script Paused/Exit) will not be shown.
    Opt("GUIResizeMode",  $GUI_DOCKSIZE+$GUI_DOCKLEFT)
    TrayCreateItem("Memory audit")
    TrayItemSetOnEvent(-1, "RAMmonitor")
EndFunc

Func RAMmonitor()
    Opt("GUIOnEventMode", 0)
    $gui = GuiCreate("Memory monitor", 544, 130,-1, -1 , $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS)
    
    $Progress_1 = CreateProgress(10, 10, 520, 20)
    $Progress_2 = CreateProgress(10, 40, 520, 20)
    $Progress_3 = CreateProgress(10, 70, 520, 20)
    $Button  = GUICtrlCreateButton("Optimize", 470, 100, 60, 25)
    GuiSetState()
    $nb=500
    While 1
        $nb=$nb+10
        If $nb > 500 Then
            $memory = MemGetStats ( )
            UpdateProgress($Progress_1, Round(100*($memory[1]-$memory[2])/$memory[1], 1), "Physical RAM: ")
            UpdateProgress($Progress_2, Round(100*($memory[3]-$memory[4])/$memory[3], 1), "Pagefile size: ")
            UpdateProgress($Progress_3, Round(100*($memory[5]-$memory[6])/$memory[5], 1), "Virtual RAM size: ")
            $nb=0
        EndIf
        Sleep(10)
        $msg = GuiGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $Button Then MemoryOptimize()
    WEnd
    GUIDelete($gui)
    Opt("GUIOnEventMode", 1)
EndFunc

Func RAMExit()
    Exit
EndFunc

;---------------------------------------------------------------
;- Enhanced Progress widget
;---------------------------------------------------------------
Func CreateProgress($x, $y, $w, $h, $Label="")
    Dim $Progress[2]
    $Progress[0] = GuiCtrlCreateProgress($x, $y, $w, $h)
    $Progress[1] = GuiCtrlCreateLabel($Label, $x, $y+3, $w, $h, $SS_CENTER )
    GUICtrlSetBkColor($Progress[1], $GUI_BKCOLOR_TRANSPARENT)
    Return $Progress
EndFunc

Func UpdateProgress($ProgressID, $Percent, $Label="")
    GUICtrlSetData($ProgressID[0], $Percent)
    GUICtrlSetData($ProgressID[1], $Label & $Percent & "%") 
EndFunc

;---------------------------------------------------------------
;- Reduce memory
;---------------------------------------------------------------
Func MemoryOptimize()
    $memory1 = MemGetStats ( )
    Do
        ReduceMemory()
        $list = ProcessList()
        For $i=1 To $list[0][0]
            If StringInStr($list[$i][0], "csrss") > 0 Or StringInStr($list[$i][0], "smss") > 0 Or StringInStr($list[$i][0], "winlogon") > 0 Or StringInStr($list[$i][0], "lsass") > 0 Then ContinueLoop
            ReduceMemory($list[$i][1])
        Next
        $memory2 = $memory1
        $memory1 = MemGetStats ( )
    Until $memory1[2] >= $memory2[2]
EndFunc
    
Func ReduceMemory($i_PID = -1)
    If $i_PID <> -1 Then
        Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])
        DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])
    Else
        Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)
    EndIf
    
    Return $ai_Return[0]
EndFunc
Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

Hi. I have added ping-pong effect to avoid window maximizing, but with maximizing enabled.

Ram.au3

Hi, I've tried your script and I found it find so can you do function for "Unfreez" process andthe memory will be normal ^^

Thanks in advance !

No-life of autoit...what could be better ?LAST SCRIPTS WITH AUTO-IT : CLICK HERE
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...