gpt4 book ai didi

python - 如何在 win7x64 上从 python 获取 PrivateUsage 内存值

转载 作者:行者123 更新时间:2023-11-28 18:51:55 26 4
gpt4 key购买 nike

我使用以下代码,它在 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())

最佳答案

我怀疑问题是由于 HANDLE 在 64 位 Windows 上是 64 位,但在 32 位 Windows 上是 32 位。默认的 ctypes 返回类型是 int,在两个系统上都是 32 位。有时它会工作,因为高 64 位恰好是正确的,但不能保证。

这与我们在 CherryPy 中通过没有显式参数和返回类型的 ctypes 调用 SetHandleInformation 时遇到的问题相同,which I described here .

您需要做的是明确指定正在调用的 Win32 函数的 argtypesrestype 属性。这是我制作的一个 processutil.py 模块,它适用于 32 位和 64 位 Windows(我还上传了一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com