Windows 7 x64: MemoryUsage Tasmanager vs. GetProcessMemoryIn

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Windows 7 x64: MemoryUsage Tasmanager vs. GetProcessMemoryIn

Post by Kurzer »

Hello,

thanks to User Taz I got to know a Windows API function today, which can show me the memory usage of my application. The procedure looks like this:

Code: Select all

Procedure.i GetProcessMemoryUsage()
	Protected.i iResult, iLib, iPID
	Protected PMC.PROCESS_MEMORY_COUNTERS
	
	iPID = GetCurrentProcess_() 
	iLib = OpenLibrary(#PB_Any, "psapi.dll")
	If iLib
		If CallFunction(iLib, "GetProcessMemoryInfo", iPID, @PMC, SizeOf(PROCESS_MEMORY_COUNTERS))
			iResult = PMC\WorkingSetSize / 1024
		EndIf
		CloseLibrary(iLib)
		ProcedureReturn iResult
	Else  
		ProcedureReturn 0
	EndIf
EndProcedure
Now I wanted to check the result of the procedures with the task manager, but notice that a completely different memory usage is displayed in the task manager. Since I was sceptical, I checked it again with the ProcessExplorer from "Sysinternals". And hey, the ProcessExplorer shows pretty much the same value as the procedure.

Image

So my question is, what value does the Windows task manager show?

btw: Yes, I compiled my programm as x86 Exe, therfore the *32 in the Taskmanager is correct.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Windows 7 x64: MemoryUsage Tasmanager vs. GetProcessMemo

Post by spikey »

kurzer wrote:So my question is, what value does the Windows task manager show?
It depends on what you have selected in the Select Columns dialog but, by default, Task Manager shows the Private Working Set, which excludes shared pages. This will generally be lower than the Working Set which includes shared pages.

Shared pages can be in use by several processes at once, these will usually be common DLLs etc. Their memory may be managed from a different source which may not be under the control of the subject process so a distinction is made.

ProcessExplorer uses slightly different descriptions and, confusingly, they overlap by name but not by value. If you select the "Commit size" column in the aforementioned dialog in Task Manager you should get a value closer to that shown by ProcExp, or "WS Private Bytes" (short for Working Set Private Bytes) in ProcExp to get the value shown by Task Manager.

GetProcessMemoryInfo terminology doesn't match exactly task manager also.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 664
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Windows 7 x64: MemoryUsage Tasmanager vs. GetProcessMemo

Post by Kurzer »

Hi Spikey,
thank you very much for the explanation. I got it now.

Image

Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2023: 56y
"Happiness is a pet." | "Never run a changing system!"
Post Reply