我使用的以下代码在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())
最佳答案
我怀疑问题是由于在64位窗口上HANDLE
是64位的,而在32位窗口上是32位的。默认的ctypes返回类型是int
,在两个系统上都是32位。有时它碰巧工作,因为高64位恰好是正确的,但没有保证。
这与CherryPy通过没有显式参数和返回类型的ctypes调用SetHandleInformation
时的问题相同,which I described here。
您需要做的是显式指定要调用的Win32函数的argtypes
和restype
属性。这里有一个processutil.py
模块可以在32位和64位窗口上运行(我也将其作为ActiveState recipe上传):
"""Functions for getting memory usage of Windows processes."""
__all__ = ['get_current_process', 'get_memory_info', 'get_memory_usage']
import ctypes
from ctypes import wintypes
GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
GetCurrentProcess.argtypes = []
GetCurrentProcess.restype = wintypes.HANDLE
SIZE_T = ctypes.c_size_t
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
_fields_ = [
('cb', wintypes.DWORD),
('PageFaultCount', wintypes.DWORD),
('PeakWorkingSetSize', SIZE_T),
('WorkingSetSize', SIZE_T),
('QuotaPeakPagedPoolUsage', SIZE_T),
('QuotaPagedPoolUsage', SIZE_T),
('QuotaPeakNonPagedPoolUsage', SIZE_T),
('QuotaNonPagedPoolUsage', SIZE_T),
('PagefileUsage', SIZE_T),
('PeakPagefileUsage', SIZE_T),
('PrivateUsage', SIZE_T),
]
GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
GetProcessMemoryInfo.argtypes = [
wintypes.HANDLE,
ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
wintypes.DWORD,
]
GetProcessMemoryInfo.restype = wintypes.BOOL
def get_current_process():
"""Return handle to current process."""
return GetCurrentProcess()
def get_memory_info(process=None):
"""Return Win32 process memory counters structure as a dict."""
if process is None:
process = get_current_process()
counters = PROCESS_MEMORY_COUNTERS_EX()
ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
ctypes.sizeof(counters))
if not ret:
raise ctypes.WinError()
info = dict((name, getattr(counters, name))
for name, _ in counters._fields_)
return info
def get_memory_usage(process=None):
"""Return this process's memory usage in bytes."""
info = get_memory_info(process=process)
return info['PrivateUsage']
if __name__ == '__main__':
import pprint
pprint.pprint(get_memory_info())
关于python - 如何在Win7x64上从python获取PrivateUsage内存值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11669335/