简体   繁体   English

使用Python获取Windows中计算机的内存使用情况

[英]Get memory usage of computer in Windows with Python

如何判断Windows XP上运行的Python计算机的整体内存使用情况?

You can also just call GlobalMemoryStatusEx() (or any other kernel32 or user32 export) directly from python: 您也可以直接从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))

Not necessarily as useful as WMI in this case, but definitely a nice trick to have in your back pocket. 在这种情况下,它不一定像WMI一样有用,但绝对是您后兜里的好招。

You'll want to use the wmi module. 您将要使用wmi模块。 Something like this: 像这样:

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"

You can query the performance counters in WMI. 您可以在WMI中查询性能计数器。 I've done something similar but with disk space instead. 我做过类似的事情,但是用了磁盘空间。

A very useful link is the Python WMI Tutorial by Tim Golden . 一个非常有用的链接是Tim Golden撰写Python WMI Tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM