-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Windows] Incorrect values in swap_memory - percent and used #2431
Comments
If the value you want is "committed", then that accurately represents it. However, that doesn't actually measure the amount used, consistently over-estimating and leading to calculations where swap used exceeds swap total. See #2074 for several examples where math as you propose produced those results. Windows reserves memory (decrementing "virtual_free" which is the sum of phsyical and swap free) before it even allows a program to proceed past the Presently psutil reports the percentage that comes from the Windows Performance counter. Rather than trying to do math from task manager, open up I humbly submit that is the "truth" that psutil should (and does) match. The same value in WMI can be calculated from Win32_PageFileUsage, which is also used to unit-test the PDH counter code, where
This interpretation exactly matches how Linux calculates swap file usage, ignoring the aggressive/pre-emptive over-allocation (commit limit) that you see on Task Manager. Given that
It's not by accident, it's rather intentional. Percentage is a fraction between 0 and 1, and only gains the 0 to 100 interpretation when the percent symbol is used as a "unit". Try opening an Excel spreadsheet and typing Yes, I understand that a variable named "percent" is expected to be from 0 to 100 and used that assumption in 6dc64c4 but realized unit tests were failing because TLDR:
|
The 13.0 GB is not physical memory used, it's the total virtual memory, the combined physical + swap "in use". The 0.82 GB swap used is a portion of the 13.0GB; you have 12.18GB of physical memory used. This combined 13.0 GB is a fraction of the 18.4GB physical+swap "committed", which is a fraction of the 64.5GB physical+swap "total". |
So in the Windows 10 case it's just an accident that small number of used memory I get (0.5125GB) resembles calculated used (I guess commited) memory (53.104GB) divided by 100 times and percent 0.2% is actually correct and shouldn't be 17.494%?
I'm not sure if it's true, since 13.0GB is the same number you see in this another window of task manager and I've never seen it going beyond 15.7GB (which it's physical limit) - though this what it would do if it had also swap in use. Here's example - program is allocating 1 gb multiple times and it's 1 GB is adding to "in use" until there is no more physical memory and it switches to swap file. Btw is this also a case of "reserved" and not "used" memory? Taskmgr_nRBB6oVMj4.mp4 |
Without a full description of actual system values (physical memory size, swap file size) I can only go on what's in the screen cap. But the overall point here is that Windows memory stats on the Task Manager reference the "commit limit" and "committed" memory. As long as you have free physical memory, it's probably going to be used, and you'll have minimal swap file usage. Only when you exceed physical memory limit does Windows have to decide what to keep in RAM and what to put on disk in the swap file; programs don't know the difference, and Windows will page swap as needed to optimize the actual location of the same virtual address. I've watched your video but I still don't know your actual physical memory limit or how big your swap file is to give context on the interpreation. But generally it looks like your From a Task Manager perspective:
I honestly don't trust most Task Manager displays. Even for program memory usage, you have to go into "Working Set" and the default shown isn't even the one you want, it's another click away. It's like they intentionally want to hide the actual memory used. Resource Monitor or PerfMon give actual counter values; Task Manager is an attempt to show you a limited amount of information for a very high level idea of resource usage. |
Not sure if that helps but this system has 192GB RAM + 292GB swap, on the screenshot it's used 116 GB RAM as RAM and 76GB RAM as shared VRAM (can't see it on the screenshot, it's visible only on GPU tab), so all RAM is used and it should have started to use swap file - and that it's just using 0.5GB swap file in that case, as Okay, even outside task manager, just within import psutil
gb = 2**30
to_gb = lambda b: b/gb
m = []
for i in range(30):
m.append(bytearray(gb))
print(to_gb(psutil.virtual_memory().used), to_gb(psutil.swap_memory().used))
# 9.743854522705078 1.6344375610351562
# 10.762199401855469 1.6344375610351562
# 11.760143280029297 1.6344375610351562
# 12.759044647216797 1.6344375610351562
# 13.756885528564453 1.6343154907226562
# 14.759471893310547 1.6343154907226562
# 15.367023468017578 1.6166534423828125
# 15.36703872680664 1.6166534423828125
# 15.3673095703125 1.6155014038085938
# 15.367111206054688 1.6155014038085938
# 15.36712646484375 1.6150856018066406
# 15.366615295410156 1.6150856018066406
# 15.367206573486328 1.6149635314941406
# 15.696582794189453 1.6122550955042243
# 15.631362915039062 1.6257438659667969
# 15.685523986816406 1.6257438659667969
# 15.658435821533203 1.6238098135218024
# 15.672370910644531 1.6848297119140625
# 15.639583587646484 1.687057494185865
# 15.672103881835938 1.8699607839807868
# 15.388587951660156 2.4693870544433594
# 15.384143829345703 2.745319366455078
# 15.414592742919922 2.7424049377441406
# 15.646942138671875 2.7895278930664062
# 15.66757583618164 3.0128555297851562
# 15.708641052246094 3.005725860595703
# 15.686126708984375 3.0018234252929688
# 15.641899108886719 3.02587890625
# 15.6419677734375 3.1997299194335938
# 15.683547973632812 3.2514152517542243 |
The 116GB "in use" on the screen shot doesn't differentiate between physical or swap memory. From the OS perspective, there is 484GB of addressable memory. Programs don't care whether the page they are accessing is in RAM or on disk (and in fact it may move back and forth between them without the program knowing or caring). I'm not sure (I doubt it) whether GPU memory is relevant to this whole discussion.
The 116 GB "in use" does not exceed the 192 GB of physical RAM. There is still no need to use the swap file. Allocate memory to programs to the point that "in use" exceeds physical RAM size and you will see an increase in swap file usage. Reserve memory to the point where "commit limit" reaches total virtual space (167/484 becomes 484/484) and you will see swap file get bigger to make it 484/512 (or something larger)).
No. "Used" has zero relevance to the location of the memory. "Used" memory might be in RAM or it might be on disk. Individual pages might move back and forth between them all while remaining being used.
Correct. "Used" does not include the commit limit. The commit limit is a Windows-only thing for ultra-conservative safety to prevent OOM crashes of the OS, and psutil tries to keep the "used" interpretation consistent across operating systems. |
But isn't the last snippet I've showed is making a program to use 30 GB of memory and some of those 30 GB are not present neither in Or I'm still missing something in definition of "used" memory and there is something else need to be done to make it "used" and not "reserved"?
From what I've seen swap file is never really increases until user will increase it in the system, e.g. if I try to allocate more than 64 GB I have I get MemoryError. |
It's confusing to me because your original screencaps showed two different systems, one in English with 64.5GB total virtual memory, and another system with 484; you've alternated between talking about the larger system with 192GB RAM + 292GB swap and then posting a program that seems to max out physical at a much lower number, that you still haven't told me what that physical memory limit is. I do not know the fine-grained details of what counts as "reserved" vs. "used" at the operating system level, I am only interpreting what I read in the documentation. I attempted to summarize this whole thing succinctly in these comments in psutil source related to the code we're discussing: Lines 257 to 263 in c034e66
TLDR:
There's a check box to allow Windows to "Automatically manage paging file size". You clearly don't have that box checked. If you did your paging file would grow and avoid the MemoryError. |
Sorry, this was the one with 16GB RAM + 48 GB swap, so 30 GB exceeds the RAM here and starting to use swap memory. Would you agree in this case that this 30GB is actually used but current metrics miss some part of it?
There is a shared GPU memory when GPU can use up to 50% of RAM as VRAM (but it seems to never use swap file though) and it never appeares to be "in use" in task manager though it is used 🤔 . I guess it's a separate subject and it can make everything even more confusing so let's avoid it. And sorry if this conversation is getting too long, it's been very insightful for me so far 😄 |
Got it.
I don't know the details of what counts as "used" vs. "committed". I do know the interpretation of "used" is consistent with the Task Manager "In Use" value which is a total of both physical and swap "in use", and I do know that the "committed" total isn't represented anywhere in psutil. This link may be helpful in explaining technical details: https://scorpiosoftware.net/2023/04/12/memory-information-in-task-manager |
So a correction to my earlier comments. It does appear the Task Manager "in use" is only the physical. However, the comments about "Commit Limit" remain. So it appears Task Manager displays these values:
|
Summary
Description
I used this snippet for tests:
And here's the results I've got on py 3.11 + windows 11:
From what I see in task manager number 5.35GB is much better resembles the truth (as you an see in task manager 13.0GB of physical memory in use and 18.4GB of all system memory in use, which gives us 18.4 - 13.0 = 5.4 GB). So manual calculation works better in this case than the current way that uses the percent.
Results from Windows 11 + python 3.12:
Manual calculations are still much more accurate.
I've also tried the same script on Windows 10 + python 3.12:
It seems that there is some other issue at play. Manual calculations and percent calculations looks similar and you can see from the screenshot below that percent calculations are actually more accurate (167-116=51GB) BUT
swap_percent
seems divided by 100 by accident, so correctswap_percent()
would be 17.494%... giving us 51.252 gb, instead we get 0.2% and 0.5125 GB.As a workaround on this machine I currently replaced
percentswap = cext.swap_percent()
withpercentswap = cext.swap_percent() * 100
.As this issue is probably related to #2160, ping @dbwiddis
The text was updated successfully, but these errors were encountered: