Skip to content

Commit

Permalink
Use MEMORYSTATUSEX values for (never used) virtual memory stats
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Oct 20, 2022
1 parent 5ca7449 commit 9140ee6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,15 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
*/
static PyObject *
psutil_virtual_mem(PyObject *self, PyObject *args) {
size_t totalPhys, availPhys, totalPageFile, availPageFile, pageSize;
size_t totalPhys, availPhys, totalPageFile, usedPageFile, pageSize;
PERFORMANCE_INFORMATION perfInfo;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);

if (! GlobalMemoryStatusEx(&memInfo)) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) {
PyErr_SetFromWindowsErr(0);
return NULL;
Expand All @@ -620,25 +627,25 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
totalPhys = perfInfo.PhysicalTotal * pageSize;
availPhys = perfInfo.PhysicalAvailable * pageSize;
totalPageFile = perfInfo.CommitLimit * pageSize;
availPageFile = totalPageFile - perfInfo.CommitTotal * pageSize;
usedPageFile = perfInfo.CommitTotal * pageSize;
// PERFORMANCE_INFORMATION values are defined as SIZE_T which on 64bits
// is an (unsigned long long) and on 32bits is an (unsigned int).
#if defined(_WIN64)
return Py_BuildValue("(LLLLLL)",
(unsigned long long) totalPhys,
(unsigned long long) availPhys,
(unsigned long long) totalPageFile,
(unsigned long long) availPageFile,
(unsigned long long) (totalPhys + totalPageFile),
(unsigned long long) (availPhys + availPageFile));
(unsigned long long) (totalPageFile - usedPageFile),
memInfo.ullTotalVirtual,
memInfo.ullAvailVirtual);
#else
return Py_BuildValue("(IIIIII)",
return Py_BuildValue("(IIIILL)",
(unsigned int) totalPhys,
(unsigned int) availPhys,
(unsigned int) totalPageFile,
(unsigned int) availPageFile,
(unsigned int) (totalPhys + totalPageFile),
(unsigned int) (availPhys + availPageFile));
(unsigned int) (totalPageFile - usedPageFile),
memInfo.ullTotalVirtual,
memInfo.ullAvailVirtual);
#endif
}

Expand Down

0 comments on commit 9140ee6

Please sign in to comment.