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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vsub
Posts: 541
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: 6222
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: 541
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: 6222
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: 3183
Joined: 30 Sep 2013, 01:33
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] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

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

21 May 2020, 05:15

jNizM wrote:
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
}
The results are different from the task manager. I wonder why?
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

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

21 May 2020, 07:49

It depends on what the column in the task manager you need. There is «Working set», there is «Private working set», there are other.
vsub
Posts: 541
Joined: 29 Sep 2015, 03:39

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

21 May 2020, 08:47

I was looking for the Private bytes and it works fine
I have a program that takes a lot of time until it actually displays anything when you try to run it
Once it reaches certain amount of mb of private bytes,then it always displays a screen(around 20 seconds later)
So I use that code to check the current usage and,use a calculation to make the numbers I get like percentage(0 to 100)and add the result to a progressbar
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

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

21 May 2020, 09:17

The Private Working Set is usually used to evaluate process memory.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

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

25 May 2020, 01:42

For more information you can use SystemProcessInformation (https://www.autohotkey.com/boards/viewtopic.php?p=268985)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

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

10 Apr 2023, 09:29

How to get the memory as shown under Windows task manager? Is it possible to just specify a process name (e.g. vivaldi.exe) and the script will automatically determine what the parent process is and get the memory info of the parent + child processes?
image.png
image.png (73.86 KiB) Viewed 791 times
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

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

10 Apr 2023, 10:10

Code: Select all

for Process in ComObjGet("winmgmts:").ExecQuery("select * from Win32_Process where name = 'vivaldi.exe'")
	sum += GetProcessMemoryUsage(Process.ProcessId)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], MrHue, Rohwedder, Rxbie, songdg and 378 guests