This is an excellent starting place, but as the last author noted, the "Working Set" is virtually worthless as an indicator of memory consumption. I could give you a long boring explanation, but the sort answer is that you want "Virtual Size" or "Private Bytes" (same thing, different versions of Windows).
I extended the example to use PROCESS_MEMORY_COUNTERS_EX, which adds Private Bytes. Use GetProcessMemory_All for this.
I also made a simpler version that returns JUST the Private Bytes, in B, K or M. It's the one I use most. Call GetProcessMemory_Private with the process name, and an optional parameter indicating the unit of measure (default KB).
To use this, just save it as C:\Program Files\AutoHotkey\Lib\GetProcessMemory.ahk, and call the functions.
Code:
;=============================================================================================================
; Func: GetProcessMemory_Private
; Get the number of private bytes used by a specified process. Result is in K by default, but can also be in
; bytes or MB.
;
; Params:
; ProcName - Name of Process (e.g. Firefox.exe)
; Units - Optional Unit of Measure B | K | M. Defaults to K (Kilobytes)
;
; Returns:
; Private bytes used by the process
;-------------------------------------------------------------------------------------------------------------
GetProcessMemory_Private(ProcName, Units="K") {
Process, Exist, %ProcName%
pid := Errorlevel
; get process handle
hProcess := DllCall( "OpenProcess", UInt, 0x10|0x400, Int, false, UInt, pid )
; get memory info
PROCESS_MEMORY_COUNTERS_EX := VarSetCapacity(memCounters, 44, 0)
DllCall( "psapi.dll\GetProcessMemoryInfo", UInt, hProcess, UInt, &memCounters, UInt, PROCESS_MEMORY_COUNTERS_EX )
DllCall( "CloseHandle", UInt, hProcess )
SetFormat, Float, 0.0 ; round up K
PrivateBytes := NumGet(memCounters, 40, "UInt")
if (Units == "B")
return PrivateBytes
if (Units == "K")
Return PrivateBytes / 1024
if (Units == "M")
Return PrivateBytes / 1024 / 1024
}
;=============================================================================================================
; Func: GetProcessMemory_All
; Get all Process Memory Usage Counters. Mimics what's shown in Task Manager.
;
; Params:
; ProcName - Name of Process (e.g. Firefox.exe)
;
; Returns:
; String with all values in KB as one big string. Use a Regular Expression to parse out the value you want.
;-------------------------------------------------------------------------------------------------------------
GetProcessMemory_All(ProcName) {
Process, Exist, %ProcName%
pid := Errorlevel
; get process handle
hProcess := DllCall( "OpenProcess", UInt, 0x10|0x400, Int, false, UInt, pid )
; get memory info
PROCESS_MEMORY_COUNTERS_EX := VarSetCapacity(memCounters, 44, 0)
DllCall( "psapi.dll\GetProcessMemoryInfo", UInt, hProcess, UInt, &memCounters, UInt, PROCESS_MEMORY_COUNTERS_EX )
DllCall( "CloseHandle", UInt, hProcess )
list := "cb,PageFaultCount,PeakWorkingSetSize,WorkingSetSize,QuotaPeakPagedPoolUsage"
. ",QuotaPagedPoolUsage,QuotaPeakNonPagedPoolUsage,QuotaNonPagedPoolUsage"
. ",PagefileUsage,PeakPagefileUsage,PrivateUsage"
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
}