我使用以下代码在 WinXPx32 上运行良好,但在 Win7x64 上返回 0。我知道 psutil 库也会返回它,但我需要一些可以在没有额外依赖项的情况下运行的东西,ctypes 和 win32api 很好。我也尝试过 Kernel32.K32GetProcessMemoryInfo ,结果相同。
import ctypes
psapi = ctypes.windll.psapi
Kernel32 = ctypes.windll.Kernel32
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
_fields_ = [("cb", ctypes.c_ulong),
("PageFaultCount", ctypes.c_ulong),
("PeakWorkingSetSize", ctypes.c_size_t),
("WorkingSetSize", ctypes.c_size_t),
("QuotaPeakPagedPoolUsage", ctypes.c_size_t),
("QuotaPagedPoolUsage", ctypes.c_size_t),
("QuotaPeakNonPagedPoolUsage", ctypes.c_size_t),
("QuotaNonPagedPoolUsage", ctypes.c_size_t),
("PagefileUsage", ctypes.c_size_t),
("PeakPagefileUsage", ctypes.c_size_t),
("PrivateUsage", ctypes.c_size_t),
]
def GetProcessPrivateUsage():
mem_struct = PROCESS_MEMORY_COUNTERS_EX()
p_handle = Kernel32.GetCurrentProcess()
b = psapi.GetProcessMemoryInfo(p_handle, ctypes.byref(mem_struct), ctypes.sizeof(mem_struct))
print(b)
return mem_struct.PrivateUsage
print(GetProcessPrivateUsage())