Skip to content

Commit

Permalink
Use system-level values for Windows virtual memory
Browse files Browse the repository at this point in the history
Submitting this as a draft PR as I believe it will address giampaolo#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 <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Oct 20, 2022
1 parent 669b672 commit 7053eab
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,18 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
*/
static PyObject *
psutil_virtual_mem(PyObject *self, PyObject *args) {
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);

if (! GlobalMemoryStatusEx(&memInfo)) {
PERFORMANCE_INFORMATION perfInfo;
if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
return Py_BuildValue("(LLLLLL)",
memInfo.ullTotalPhys, // total
memInfo.ullAvailPhys, // avail
memInfo.ullTotalPageFile, // total page file
memInfo.ullAvailPageFile, // avail page file
memInfo.ullTotalVirtual, // total virtual
memInfo.ullAvailVirtual); // avail virtual
perfInfo.PhysicalTotal * perfInfo.PageSize, // total
perfInfo.PhysicalAvailable * perfInfo.PageSize, // avail
(perfInfo.CommitLimit - perfInfo.PhysicalTotal) * perfInfo.PageSize, // total page file
(perfInfo.CommitLimit - perfInfo.CommitTotal - perfInfo.PhysicalAvailable) * perfInfo.PageSize, // avail page file
perfInfo.CommitLimit * perfInfo.PageSize, // total virtual
(perfInfo.CommitLimit - perfInfo.CommitTotal) * perfInfo.PageSize); // avail virtual
}


Expand Down

0 comments on commit 7053eab

Please sign in to comment.