How can I get the memory usage of a process? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
vsub
Posts: 188
Joined: 29 Sep 2015, 03:39

How can I get the memory usage of a process?

18 Mar 2019, 04:54

I tried this code but it always returns 0

Code: Select all

Msgbox,% MemUsage("firefox.exe")
MemUsage(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
}
I need to scan the process every second until a window appear(the program takes at least 30 seconds until it actually appears and I want to have some kind of loading indicator because sometimes the program crashes at startup without showing anything)

I tried running the script as admin but I also get 0(I am using the x64 version of ahk and firefox.exe(just an example)is x64 process)
swagfag
Posts: 3273
Joined: 11 Jan 2017, 17:59

Re: How can I get the memory usage of a process?

18 Mar 2019, 05:28

sizeof(SIZE_T) is 8 on x64, so ur struct is too small. it should be 80
the offset should be 56 or 72
vsub
Posts: 188
Joined: 29 Sep 2015, 03:39

Re: How can I get the memory usage of a process?

18 Mar 2019, 06:10

Thanks for the help but that doesn't say anything to me,I just find this code by using google.
What exactly do I need to change

And btw even if i try getting an info for a x32 process,I still get 0
swagfag
Posts: 3273
Joined: 11 Jan 2017, 17:59

Re: How can I get the memory usage of a process?

18 Mar 2019, 06:22

Code: Select all

 VarSetCapacity(memCounters, 80, 0)

Code: Select all

NumGet(memCounters, 72, "UInt")
User avatar
jNizM
Posts: 2574
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: How can I get the memory usage of a process?  Topic is solved

18 Mar 2019, 06:52

Quick and Dirty

Usage:

Code: Select all

OwnPID := DllCall("GetCurrentProcessId")
MsgBox % GetProcessMemoryUsage(OwnPID) " MB"

Process, Exist, % "firefox.exe"
MsgBox % GetProcessMemoryUsage(ErrorLevel) " MB"
Function:

Code: Select all

GetProcessMemoryUsage(ProcessID)
{
	static PMC_EX, size := NumPut(VarSetCapacity(PMC_EX, 8 + A_PtrSize * 9, 0), PMC_EX, "uint")

	if (hProcess := DllCall("OpenProcess", "uint", 0x1000, "int", 0, "uint", ProcessID)) {
		if !(DllCall("GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
			if !(DllCall("psapi\GetProcessMemoryInfo", "ptr", hProcess, "ptr", &PMC_EX, "uint", size))
				return (ErrorLevel := 2) & 0, DllCall("CloseHandle", "ptr", hProcess)
		DllCall("CloseHandle", "ptr", hProcess)
		return Round(NumGet(PMC_EX, 8 + A_PtrSize * 8, "uptr") / 1024**2, 2)
	}
	return (ErrorLevel := 1) & 0
}
[AHK] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1909) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, genaro, Google [Bot], Noobz and 41 guests