如何在Windows上找到当前的系统缓存大小?

2024-05-16 21:21:43 发布

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

搜索了高和低,但找不到API调用来检索Windows上(文件)系统缓存的当前大小。在

GlobalMemoryStatusEx-检索总计、可用、已用和交换状态。在

GetSystemFileCacheSize-返回可能的最小值和最大值,这不是很有用。在

我还尝试了Windows扩展,它返回了以下无用的数字。看起来可能是1mb到2gb?在

>>> import win32api
>>> win32api.GetSystemFileCacheSize()
(1048576L, 2143289344L, 0L)

获取此信息的正确API调用是什么?我看到它在任务管理器中可用,所以它一定在那里的某个地方?这是屏幕截图和我要查找的数字:

enter image description here

希望使用Python解决方案,但C/C++会有很大帮助。在


Tags: 文件importapi信息管理器屏幕windows状态
1条回答
网友
1楼 · 发布于 2024-05-16 21:21:43

我终于明白了:

import ctypes
psapi = ctypes.WinDLL('psapi')

class PERFORMANCE_INFORMATION(ctypes.Structure):
    ''' Struct for Windows .GetPerformanceInfo().
        http://msdn.microsoft.com/en-us/library/ms683210
    '''

    _DWORD = ctypes.c_ulong
    _SIZE_T = ctypes.c_size_t

    _fields_ = [
        ('cb', _DWORD),
        ('CommitTotal', _SIZE_T),
        ('CommitLimit', _SIZE_T),
        ('CommitPeak', _SIZE_T),
        ('PhysicalTotal', _SIZE_T),
        ('PhysicalAvailable', _SIZE_T),
        ('SystemCache', _SIZE_T),
        ('KernelTotal', _SIZE_T),
        ('KernelPaged', _SIZE_T),
        ('KernelNonpaged', _SIZE_T),
        ('PageSize', _SIZE_T),
        ('HandleCount', _DWORD),
        ('ProcessCount', _DWORD),
        ('ThreadCount', _DWORD),
    ]

    def __init__(self, getinfo=True, *args, **kwds):
        super(PERFORMANCE_INFORMATION, self).__init__(
              ctypes.sizeof(self), *args, **kwds)
        if (getinfo and not
            psapi.GetPerformanceInfo(ctypes.byref(self), 
                                     self.cb)):
            raise WinError()

    @property
    def cache_info(self):
        return self.SystemCache * self.PageSize

def get_cache_info():
    return PERFORMANCE_INFORMATION().cache_info

if __name__ == '__main__':
    print(get_cache_info())

相关问题 更多 >