Skip to content

Instantly share code, notes, and snippets.

@shyoo
Forked from cheeaun/close-app-mem.ahk
Last active December 15, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyoo/5272758 to your computer and use it in GitHub Desktop.
Save shyoo/5272758 to your computer and use it in GitHub Desktop.
I forked and added ask-before-reboot firefox.exe
Loop {
app = firefox.exe
; firefox keep consumes up your memory. sometimes rebooting is good
mem := GetProcessMemoryInfo(app)
Menu, tray, tip, %app% %mem% KB
; if memory is consumed more than 1G ask to user and restart
if mem > 1000000
{
MsgBox, Okay, it's time to restart your firefox. Click OK to reboot firefox.
Process, close, %app%
TrayTip, %app% closed, %mem% KB used.
ProcessWait( "firefox.exe", 1000 ) ; wait for 1000 sec to kill firefox.
; now start firefox
Run, "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
}
Sleep, 1000
}
; http://www.autohotkey.com/forum/viewtopic.php?p=223061#223061
GetProcessMemoryInfo( pname )
{
Process, Exist, %pname%
pid := Errorlevel
; get process handle
hProcess := DllCall( "OpenProcess", UInt, 0x10|0x400, Int, false, UInt, pid )
; get memory info
VarSetCapacity( memCounters, 40, 0 )
DllCall( "psapi.dll\GetProcessMemoryInfo", UInt, hProcess, UInt, &memCounters, UInt, 40 )
DllCall( "CloseHandle", UInt, hProcess )
list = cb,PageFaultCount,PeakWorkingSetSize,WorkingSetSize,QuotaPeakPagedPoolUsage
,QuotaPagedPoolUsage,QuotaPeakNonPagedPoolUsage,QuotaNonPagedPoolUsage
,PagefileUsage,PeakPagefileUsage
/*
cb := NumGet( memCounters, 0, "UInt" )
PageFaultCount := NumGet( memCounters, 4, "UInt" )
PeakWorkingSetSize := NumGet( memCounters, 8, "UInt" )
WorkingSetSize := NumGet( memCounters, 12, "UInt" )
QuotaPeakPagedPoolUsage := NumGet( memCounters, 16, "UInt" )
QuotaPagedPoolUsage := NumGet( memCounters, 20, "UInt" )
QuotaPeakNonPagedPoolUsage := NumGet( memCounters, 24, "UInt" )
QuotaNonPagedPoolUsage := NumGet( memCounters, 28, "UInt" )
PagefileUsage := NumGet( memCounters, 32, "UInt" )
PeakPagefileUsage := NumGet( memCounters, 36, "UInt" )
*/
n=0
Loop, Parse, list, `,
{
n+=4
SetFormat, Float, 0.0 ; round up K
this := A_Loopfield
this := NumGet( memCounters, (A_Index = 1 ? 0 : n-4), "UInt") / 1024
; omit cb
If A_Index != 1
info .= A_Loopfield . ": " . this . " K" . ( A_Loopfield != "" ? "`n" : "" )
}
; Return "[" . pid . "] " . pname . "`n`n" . info ; for everything
; Return WorkingSetSize := NumGet( memCounters, 12, "UInt" ) / 1024 . " K" ; what Task Manager shows
Return PagefileUsage := NumGet( memCounters, 32, "UInt" ) / 1024 ; what Task Manager shows
}
; from http://www.autohotkey.com/board/topic/58708-how-to-check-if-process-exists/
ProcessWait(PidOrName, Timeout="") {
Process, Wait, %PidOrName%, %Timeout%
return ErrorLevel
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment