可用总RAM始终是sam

2024-09-30 19:31:26 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图通过python来获得我电脑的可用内存和总内存。这就是我现在所拥有的:

def get_memory_status():
    kernel32 = ctypes.windll.kernel32
    c_ulong = ctypes.c_ulong
    class MEMORYSTATUS(ctypes.Structure):
        _fields_ = [
            ("dwLength", c_ulong),
            ("dwMemoryLoad", c_ulong),
            ("dwTotalPhys", c_ulong),
            ("dwAvailPhys", c_ulong),
            ("dwTotalPageFile", c_ulong),
            ("dwAvailPageFile", c_ulong),
            ("dwTotalVirtual", c_ulong),
            ("dwAvailVirtual", c_ulong)
        ]
    memoryStatus = MEMORYSTATUS()
    memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
    kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))

    return (memoryStatus.dwAvailPhys, memoryStatus.dwTotalPhys)

avail, total = get_memory_status()
print avail + " " + total

如果我执行这个操作,我总是得到相同的可用RAM值和总RAM值。当我请求dwMemoryLoad时,我得到的值与在窗口的任务管理器中显示的“物理内存”相同,它是已使用RAM的百分比(不是0)。但我想要精确的字节数。我做错什么了吗?在

我不能用任何额外的图书馆,如果可以的话,我早就用了。在


Tags: 内存getstatusctypesrammemoryulongavail
1条回答
网友
1楼 · 发布于 2024-09-30 19:31:26

根据GlobalMemoryStatus MSDN entry处的注释:

On Intel x86 computers with more than 2 GB and less than 4 GB of memory, the GlobalMemoryStatus function will always return 2 GB in the dwTotalPhys member of the MEMORYSTATUS structure. Similarly, if the total available memory is between 2 and 4 GB, the dwAvailPhys member of the MEMORYSTATUS structure will be rounded down to 2 GB. If the executable is linked using the /LARGEADDRESSAWARE linker option, then the GlobalMemoryStatus function will return the correct amount of physical memory in both members.

在文章的顶部,有:

[GlobalMemoryStatus can return incorrect information. Use the GlobalMemoryStatusEx function instead.]

顺便说一句,这个函数存在于pywin32库中。在

相关问题 更多 >