在 Windows XP 上运行的 Python 如何判断计算机的总体内存使用量是多少?
11834 次
3 回答
23
您也可以直接从 python 调用 GlobalMemoryStatusEx() (或任何其他 kernel32 或 user32 导出):
import ctypes
class MEMORYSTATUSEX(ctypes.Structure):
_fields_ = [
("dwLength", ctypes.c_ulong),
("dwMemoryLoad", ctypes.c_ulong),
("ullTotalPhys", ctypes.c_ulonglong),
("ullAvailPhys", ctypes.c_ulonglong),
("ullTotalPageFile", ctypes.c_ulonglong),
("ullAvailPageFile", ctypes.c_ulonglong),
("ullTotalVirtual", ctypes.c_ulonglong),
("ullAvailVirtual", ctypes.c_ulonglong),
("sullAvailExtendedVirtual", ctypes.c_ulonglong),
]
def __init__(self):
# have to initialize this to the size of MEMORYSTATUSEX
self.dwLength = ctypes.sizeof(self)
super(MEMORYSTATUSEX, self).__init__()
stat = MEMORYSTATUSEX()
ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat))
print("MemoryLoad: %d%%" % (stat.dwMemoryLoad))
在这种情况下,它不一定像 WMI 那样有用,但绝对是一个很好的小窍门。
于 2010-01-07T01:42:13.183 回答
12
你会想要使用wmi模块。像这样的东西:
import wmi
comp = wmi.WMI()
for i in comp.Win32_ComputerSystem():
print i.TotalPhysicalMemory, "bytes of physical memory"
for os in comp.Win32_OperatingSystem():
print os.FreePhysicalMemory, "bytes of available memory"
于 2010-01-07T01:27:57.017 回答
0
您可以在 WMI 中查询性能计数器。我做了类似的事情,但使用了磁盘空间。
一个非常有用的链接是Tim Golden 的 Python WMI 教程。
于 2010-01-07T01:29:38.043 回答