Skip to content
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

Use system-level values for Windows virtual memory #2077

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,23 @@ 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)) {
unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize;
PERFORMANCE_INFORMATION perfInfo;
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
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
// values are size_t, widen to long long
pageSize = perfInfo.PageSize;
totalPhys = perfInfo.PhysicalTotal * pageSize;
availPhys = perfInfo.PhysicalAvailable * pageSize;
totalSys = perfInfo.CommitLimit * pageSize;
availSys = totalSys - perfInfo.CommitTotal * pageSize;
return Py_BuildValue("(LLLL)",
totalPhys,
availPhys,
totalSys,
availSys);
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down
5 changes: 2 additions & 3 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def getpagesize():
def virtual_memory():
"""System virtual memory as a namedtuple."""
mem = cext.virtual_mem()
totphys, availphys, totpagef, availpagef, totvirt, freevirt = mem
totphys, availphys, totsys, availsys = mem
#
total = totphys
avail = availphys
Expand All @@ -248,8 +248,7 @@ def swap_memory():
total_system = mem[2]
free_system = mem[3]

# Despite the name PageFile refers to total system memory here
# thus physical memory values need to be subtracted to get swap values
# physical memory values need to be substracted to get swap values
total = total_system - total_phys
free = min(total, free_system - free_phys)
used = total - free
Expand Down