Skip to content

Commit cd8827d

Browse files
authored
Use system-level values for Windows virtual memory (#2077)
* Use system-level values for Windows virtual memory Submitting this as a draft PR as I believe it will address #2074. I do not have either a C or Python development environment to test these changes, so they are provided in the hopes someone else can test and confirm they fix the issue. There's probably some room to simplify the code with temporary variables to reduce redundancy. For background, I have implemented precisely this logic in my own (Java-based) project [here](https://github.com/oshi/oshi/blob/3bb9eafbe2062edad4108b7b12501b1f9be27361/oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsVirtualMemory.java#L110-L118) and [here](https://github.com/oshi/oshi/blob/3bb9eafbe2062edad4108b7b12501b1f9be27361/oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGlobalMemory.java#L253-L263) following a detailed investigation in oshi/oshi#1182 Signed-off-by: Daniel Widdis <[email protected]> * Formatting, credits, change log Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: Daniel Widdis <[email protected]>
1 parent 614e911 commit cd8827d

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,3 +801,7 @@ I: 2135
801801

802802
N: Daniel Li
803803
I: 2150
804+
805+
N: Daniel Widdis
806+
W: https://github.com/dbwiddis
807+
I: 2077

HISTORY.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
*Bug tracker at https://github.com/giampaolo/psutil/issues*
22

3+
5.9.4 (IN DEVELOPMENT)
4+
======================
5+
6+
XXXX-XX-XX
7+
8+
**Bug fixes**
9+
10+
- 2077_, [Windows]: Use system-level values for `virtual_memory()`. (patch by
11+
Daniel Widdis)
12+
313
5.9.3
414
=====
515

psutil/_psutil_windows.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,20 +610,25 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
610610
*/
611611
static PyObject *
612612
psutil_virtual_mem(PyObject *self, PyObject *args) {
613-
MEMORYSTATUSEX memInfo;
614-
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
613+
unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize;
614+
PERFORMANCE_INFORMATION perfInfo;
615615

616-
if (! GlobalMemoryStatusEx(&memInfo)) {
616+
if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) {
617617
PyErr_SetFromWindowsErr(0);
618618
return NULL;
619619
}
620-
return Py_BuildValue("(LLLLLL)",
621-
memInfo.ullTotalPhys, // total
622-
memInfo.ullAvailPhys, // avail
623-
memInfo.ullTotalPageFile, // total page file
624-
memInfo.ullAvailPageFile, // avail page file
625-
memInfo.ullTotalVirtual, // total virtual
626-
memInfo.ullAvailVirtual); // avail virtual
620+
// values are size_t, widen (if needed) to long long
621+
pageSize = perfInfo.PageSize;
622+
totalPhys = perfInfo.PhysicalTotal * pageSize;
623+
availPhys = perfInfo.PhysicalAvailable * pageSize;
624+
totalSys = perfInfo.CommitLimit * pageSize;
625+
availSys = totalSys - perfInfo.CommitTotal * pageSize;
626+
return Py_BuildValue(
627+
"(LLLL)",
628+
totalPhys,
629+
availPhys,
630+
totalSys,
631+
availSys);
627632
}
628633

629634

psutil/_pswindows.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def getpagesize():
229229
def virtual_memory():
230230
"""System virtual memory as a namedtuple."""
231231
mem = cext.virtual_mem()
232-
totphys, availphys, totpagef, availpagef, totvirt, freevirt = mem
232+
totphys, availphys, totsys, availsys = mem
233233
#
234234
total = totphys
235235
avail = availphys
@@ -248,8 +248,8 @@ def swap_memory():
248248
total_system = mem[2]
249249
free_system = mem[3]
250250

251-
# Despite the name PageFile refers to total system memory here
252-
# thus physical memory values need to be subtracted to get swap values
251+
# system memory (commit total/limit) is the sum of physical and swap
252+
# thus physical memory values need to be substracted to get swap values
253253
total = total_system - total_phys
254254
free = min(total, free_system - free_phys)
255255
used = total - free

0 commit comments

Comments
 (0)