简体   繁体   English

在Windows中使用ctypes的错误虚拟内存报告

[英]incorrect virtual memory report using ctypes in windows

i am trying to get the total/available virtual memory in python using ctypes, but it seems to be returning a highly incorrect value. 我正在尝试使用ctypes在python中获取总的/可用的虚拟内存,但是它似乎返回了一个非常不正确的值。 below is the code i currently have 以下是我目前拥有的代码

import ctypes
import ctypes.wintypes as wintypes

def _get_virtual_size():
    class MEMORYSTATUSEX(ctypes.Structure):
        _fields_ = [("dwLength", wintypes.DWORD),
                    ("dwMemoryLoad", wintypes.DWORD),
                    ("ullTotalPhys", ctypes.c_uint64),
                    ("ullAvailPhys", ctypes.c_uint64),
                    ("ullTotalPageFile", ctypes.c_uint64),
                    ("ullAvailPageFile", ctypes.c_uint64),
                    ("ullTotalVirtual", ctypes.c_uint64),
                    ("ullAvailVirtual", ctypes.c_uint64),
                    ("ullAvailExtendedVirtual", ctypes.c_uint64)]

        def __init__(self):
            ctypes.Structure.__init__(self)
            self.dwLength = ctypes.sizeof(self)

    kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
    GlobalMemoryStatusEx = kernel32.GlobalMemoryStatusEx
    GlobalMemoryStatusEx.restype = wintypes.BOOL
    GlobalMemoryStatusEx.argtypes = [ctypes.POINTER(MEMORYSTATUSEX)]

    status = MEMORYSTATUSEX()
    if GlobalMemoryStatusEx(status):
        return {'totalPage':status.ullAvailPageFile,  'total':status.ullTotalVirtual, 'available':status.ullAvailVirtual}
    return None

print _get_virtual_size()

Result: {'available': 140737380003840L, 'totalPage': 13305126912L, 'total': 140737488224256L} 结果:{'available':140737380003840L,'totalPage':13305126912L,'total':140737488224256L}

Note : Using psutil the value seems to be reported correctly, but need a solution without using psutil. 注意:使用psutil,似乎可以正确报告该值,但是需要一种不使用psutil的解决方案。

import psutil
print psutil.virtual_memory()

Result: svmem(total=17114841088L, available=13436207104L, percent=21.5, used=3678633984L, free=13436207104L) 结果:svmem(总数= 17114841088L,可用= 13436207104L,百分比= 21.5,已使用= 3678633984L,空闲= 13436207104L)

How do i get the same values reported by psutil using the ctypes method. 我如何使用ctypes方法获得psutil报告的相同值。 I am currently running the above code in Python 2.7.10 64Bit on Windows 8.1 我目前在Windows 8.1上的Python 2.7.10 64Bit中运行以上代码

Any help would be appreciated. 任何帮助,将不胜感激。

Try using this code. 尝试使用此代码。 Not sure if it will work for you. 不确定是否适合您。 I'm running 32-bit Python on a 64-bit windows 7 pc {Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32} . 我在64位Windows 7 pc上运行32位Python (Python 2.7.6(默认值,2013年11月10日,19:24:18)[win32上的MSC v.1500 32位(英特尔))} I have compared the values returned to what is reported in the Performance tab of my Windows Task Manager application. 我将返回的值与Windows Task Manager应用程序的“性能”选项卡中报告的值进行了比较。

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),
        ("ullAvailExtendedVirtual", 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))
print . . . #output other fields here

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

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