基于以下链接:MSDN Docu
我正在尝试获取当前正在使用的页面文件的大小。
以下是我获取价值的方式:
ActualPageFileSize = ullAvailPageFile - ullTotalPageFile
AvailablePageFileSize = ullAvailPageFile - ullAvailPhys
UsedPageFile = ActualPageFileSize - AvailablePageFileSize
以上计算基于另一篇文章: Another post
我现在遇到的问题是,有时AvailablePageFileSize的值大于ActualPageFileSize的值,因此返回UsedPageFile的负值。
有没有人遇到过这个?任何想法都将不胜感激。
提前谢谢!
P.S。
示例代码:
#define DIV 1024
void _tmain()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
double committedBytes = 1;
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT("GlobalMemoryStatusEx\n"));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT(" %PhysUsed : %ld\n"), statex.dwMemoryLoad);
_tprintf (TEXT(" ullTotalPhys : %I64d (%I64d MB)\n"), statex.ullTotalPhys, statex.ullTotalPhys / (DIV*DIV));
_tprintf (TEXT(" ullAvailPhys : %I64d (%I64d MB)\n"), statex.ullAvailPhys, statex.ullAvailPhys / (DIV*DIV));
_tprintf (TEXT(" ullUsedPhys : %I64d (%I64d MB)\n"), statex.ullTotalPhys - statex.ullAvailPhys, (statex.ullTotalPhys - statex.ullAvailPhys) / (DIV*DIV));
_tprintf (TEXT(" --------------------------\n"));
_tprintf (TEXT(" Phys Avail + Used = %I64d (%I64d MB)\n"), statex.ullAvailPhys + (statex.ullTotalPhys - statex.ullAvailPhys), (statex.ullAvailPhys + (statex.ullTotalPhys - statex.ullAvailPhys)) / (DIV*DIV));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT(" ullTotalPageFile : %I64d (%I64d MB)\n"), statex.ullTotalPageFile, statex.ullTotalPageFile / (DIV*DIV));
_tprintf (TEXT(" ullAvailPageFile : %I64d (%I64d MB)\n"), statex.ullAvailPageFile, statex.ullAvailPageFile / (DIV*DIV));
_tprintf (TEXT(" ullUsedPageFile : %I64d (%I64d MB)\n"), statex.ullTotalPageFile - statex.ullAvailPageFile, (statex.ullTotalPageFile - statex.ullAvailPageFile) / (DIV*DIV));
_tprintf (TEXT(" --------------------------\n"));
_tprintf (TEXT(" Page Avail + Used = %I64d (%I64d MB)\n"), statex.ullAvailPageFile + (statex.ullTotalPageFile - statex.ullAvailPageFile), (statex.ullAvailPageFile + (statex.ullTotalPageFile - statex.ullAvailPageFile)) / (DIV*DIV));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT(" ActualTotalPageFile : %I64d (%I64d MB)\n"), statex.ullTotalPageFile - statex.ullTotalPhys, (statex.ullTotalPageFile - statex.ullTotalPhys) / (DIV*DIV));
_tprintf (TEXT(" ActualAvailPageFile : %I64d (%I64d MB)\n"), statex.ullAvailPageFile - statex.ullAvailPhys, (statex.ullAvailPageFile - statex.ullAvailPhys) / (DIV*DIV));
_tprintf (TEXT(" ActualPageFileUsed : %I64d (%I64d MB)\n"), (statex.ullTotalPageFile - statex.ullTotalPhys) - (statex.ullAvailPageFile - statex.ullAvailPhys), ((statex.ullTotalPageFile - statex.ullTotalPhys) - (statex.ullAvailPageFile - statex.ullAvailPhys)) / (DIV*DIV));
_tprintf (TEXT(" Variance : %I64d (%I64d MB)\n"), (statex.ullAvailPageFile - statex.ullAvailPhys) - (statex.ullTotalPageFile - statex.ullTotalPhys), ((statex.ullAvailPageFile - statex.ullAvailPhys) - (statex.ullTotalPageFile - statex.ullTotalPhys)) / (DIV*DIV));
_tprintf (TEXT(" --------------------------\n"));
_tprintf (TEXT("ActPage Avail + Used = %I64d (%I64d MB)\n"), (statex.ullAvailPageFile - statex.ullAvailPhys) + ((statex.ullTotalPageFile - statex.ullTotalPhys) - (statex.ullAvailPageFile - statex.ullAvailPhys)), ((statex.ullAvailPageFile - statex.ullAvailPhys) + ((statex.ullTotalPageFile - statex.ullTotalPhys) - (statex.ullAvailPageFile - statex.ullAvailPhys))) / (DIV*DIV));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT(" ullTotalVirtual : %I64d (%I64d MB)\n"), statex.ullTotalVirtual, statex.ullTotalVirtual / (DIV*DIV));
_tprintf (TEXT(" ullAvailVirtual : %I64d (%I64d MB)\n"), statex.ullAvailVirtual, statex.ullAvailVirtual / (DIV*DIV));
_tprintf (TEXT(" ullAvailVirtualUsed : %I64d (%I64d MB)\n"), statex.ullTotalVirtual - statex.ullAvailVirtual, (statex.ullTotalVirtual - statex.ullAvailVirtual) / (DIV*DIV));
PERFORMACE_INFORMATION perfInfo;
int ret;
ret = GetPerformanceInfo (&perfInfo,sizeof(perfInfo));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT("GetPerformanceInfo\n"));
_tprintf (TEXT("==================================================\n"));
_tprintf (TEXT(" PageSize : %d (%d MB) \n"), perfInfo.PageSize, perfInfo.PageSize / DIV);
_tprintf (TEXT(" CommitTotal : %d (%d MB) X %d = %I64d (%I64d MB)\n"), perfInfo.CommitTotal, perfInfo.CommitTotal / DIV, perfInfo.PageSize, (DWORDLONG)perfInfo.CommitTotal * perfInfo.PageSize, (DWORDLONG)(perfInfo.CommitTotal * perfInfo.PageSize) / (DIV*DIV));
_tprintf (TEXT(" CommitLimit : %d (%d MB) X %d = %I64d (%I64d MB)\n"), perfInfo.CommitLimit, perfInfo.CommitLimit / DIV, perfInfo.PageSize, (DWORDLONG)perfInfo.CommitLimit * perfInfo.PageSize, (DWORDLONG)(perfInfo.CommitLimit * perfInfo.PageSize) / (DIV*DIV));
_tprintf (TEXT(" CommitAvail : %d (%d MB) X %d = %I64d (%I64d MB)\n"), perfInfo.CommitLimit - perfInfo.CommitTotal, (perfInfo.CommitLimit - perfInfo.CommitTotal) / DIV, perfInfo.PageSize, (DWORDLONG)(perfInfo.CommitLimit - perfInfo.CommitTotal) * perfInfo.PageSize, (DWORDLONG)((perfInfo.CommitLimit - perfInfo.CommitTotal) * perfInfo.PageSize) / (DIV*DIV));
_tprintf (TEXT(" PhysicalTotal : %d (%d MB) X %d = %I64d (%I64d MB)\n"), perfInfo.PhysicalTotal, perfInfo.PhysicalTotal / DIV, perfInfo.PageSize, (DWORDLONG)perfInfo.PhysicalTotal * perfInfo.PageSize, (DWORDLONG)(perfInfo.PhysicalTotal * perfInfo.PageSize) / (DIV*DIV));
_tprintf (TEXT(" PhysicalAvail : %d (%d MB) X %d = %I64d (%I64d MB)\n"), perfInfo.PhysicalAvailable, perfInfo.PhysicalAvailable / DIV, perfInfo.PageSize, (DWORDLONG)(perfInfo.PhysicalAvailable * perfInfo.PageSize), (DWORDLONG)(perfInfo.PhysicalAvailable * perfInfo.PageSize) / (DIV*DIV));
DWORDLONG ptotal = (DWORDLONG)(perfInfo.CommitLimit - perfInfo.PhysicalTotal) * perfInfo.PageSize;
DWORDLONG pavail = (DWORDLONG)((perfInfo.CommitLimit - perfInfo.CommitTotal) - perfInfo.PhysicalAvailable) * perfInfo.PageSize;
_tprintf (TEXT("\nCommitLimit - PhysicalTotal = %I64d (%I64d MB) \n"), ptotal, (DWORDLONG)ptotal / (DIV*DIV));
_tprintf (TEXT("CommitAvail - PhysicalAvail = %I64d (%I64d MB) \n"), pavail, (DWORDLONG)pavail / (DIV*DIV));
_tprintf (TEXT("CommitUsed - %I64d (%I64d MB) \n"), (DWORDLONG)(ptotal - pavail), (DWORDLONG)(ptotal - pavail) / (DIV*DIV));
}
示例良好输出:
R:\>memtest.exe
==================================================
GlobalMemoryStatusEx
==================================================
PhysUsed : 24
ullTotalPhys : 4294430720 (4095 MB)
ullAvailPhys : 3231776768 (3082 MB)
ullUsedPhys : 1062653952 (1013 MB)
--------------------------
Phys Avail + Used = 4294430720 (4095 MB)
==================================================
ullTotalPageFile : 5032628224 (4799 MB)
ullAvailPageFile : 3932905472 (3750 MB)
ullUsedPageFile : 1099722752 (1048 MB)
--------------------------
Page Avail + Used = 5032628224 (4799 MB)
==================================================
ActualTotalPageFile : 738197504 (704 MB)
ActualAvailPageFile : 701128704 (668 MB)
ActualPageFileUsed : 37068800 (35 MB)
Variance : -37068800 (17592186044380 MB)
--------------------------
ActPage Avail + Used = 738197504 (704 MB)
==================================================
ullTotalVirtual : 2147352576 (2047 MB)
ullAvailVirtual : 2072608768 (1976 MB)
ullAvailVirtualUsed : 74743808 (71 MB)
==================================================
GetPerformanceInfo
==================================================
PageSize : 4096 (4 MB)
CommitTotal : 268487 (262 MB) X 4096 = 1099722752 (1048 MB)
CommitLimit : 1228669 (1199 MB) X 4096 = 5032628224 (703 MB)
CommitAvail : 960182 (937 MB) X 4096 = 3932905472 (3750 MB)
PhysicalTotal : 1048445 (1023 MB) X 4096 = 4294430720 (4095 MB)
PhysicalAvail : 789000 (770 MB) X 4096 = 3231744000 (3082 MB)
CommitLimit - PhysicalTotal = 738197504 (704 MB)
CommitAvail - PhysicalAvail = 701161472 (668 MB)
CommitUsed - 37036032 (35 MB)
R:\>
示例错误输出:
U:\>memtest.exe
==================================================
GlobalMemoryStatusEx
==================================================
PhysUsed : 35
ullTotalPhys : 3220688896 (3071 MB)
ullAvailPhys : 2087751680 (1991 MB) <--- Should be less than ullAvailPageFile
ullUsedPhys : 1132937216 (1080 MB)
--------------------------
Phys Avail + Used = 3220688896 (3071 MB)
==================================================
ullTotalPageFile : 3757559808 (3583 MB)
ullAvailPageFile : 1925316608 (1836 MB) <-- Shouldbe greater than ullAvailPhys
ullUsedPageFile : 1832243200 (1747 MB)
--------------------------
Page Avail + Used = 3757559808 (3583 MB)
==================================================
ActualTotalPageFile : 536870912 (512 MB)
ActualAvailPageFile : -162435072 (17592186044261 MB) <-- This is ullAvailPageFile - ullAvailPhys
ActualPageFileUsed : 699305984 (666 MB)
Variance : -699305984 (17592186043749 MB)
--------------------------
ActPage Avail + Used = 536870912 (512 MB)
==================================================
ullTotalVirtual : 2147352576 (2047 MB)
ullAvailVirtual : 2139230208 (2040 MB)
ullAvailVirtualUsed : 8122368 (7 MB)
==================================================
GetPerformanceInfo
==================================================
PageSize : 4096 (4 MB)
CommitTotal : 447325 (436 MB) X 4096 = 1832243200 (1747 MB)
CommitLimit : 917373 (895 MB) X 4096 = 3757559808 (3583 MB)
CommitAvail : 470048 (459 MB) X 4096 = 1925316608 (1836 MB)
PhysicalTotal : 786301 (767 MB) X 4096 = 3220688896 (3071 MB)
PhysicalAvail : 509698 (497 MB) X 4096 = 2087723008 (1991 MB)
CommitLimit - PhysicalTotal = 536870912 (512 MB)
CommitAvail - PhysicalAvail = 17592023638016 (16777061 MB)
CommitUsed - -17591486767104 (17592169267866 MB)
U:\>
答案 0 :(得分:0)
下一个代码的工作原理是从win2000到最新的win10 x64。关于弃用,未记录等:
NtQuerySystemInformation将来可能会被更改或不可用 Windows版本。
我已经读了15年了。 ntdll api将来/将被删除 - 我的个人选择是否定的。决不。这是基础原生api和基于此的所有所谓的win32 api。但这只是我的选择。如何最小化这对所有现有的Windows版本都很好。原样(我肯定会在新版本中使用)
typedef struct SYSTEM_PAGEFILE_INFORMATION
{
ULONG NextEntryOffset;
ULONG TotalSize;
ULONG TotalInUse;
ULONG PeakUsage;
UNICODE_STRING PageFileName;
} *PSYSTEM_PAGEFILE_INFORMATION;
NTSTATUS GetPageFilesInfo()
{
static volatile UCHAR guz;
PVOID stack = alloca(guz);
ULONG cb = 0, rcb = 2*sizeof(SYSTEM_PAGEFILE_INFORMATION) + 64* sizeof(WCHAR);
union {
PVOID buf;
PSYSTEM_PAGEFILE_INFORMATION pspfi;
PBYTE pb;
};
NTSTATUS status;
do
{
if (cb < rcb)
{
cb = RtlPointerToOffset(buf = alloca(rcb - cb), stack);
}
if (0 <= (status = NtQuerySystemInformation(SystemPageFileInformation, buf, cb, &rcb)))
{
ULONG NextEntryOffset = 0;
do
{
pb += NextEntryOffset;
DbgPrint("%ukb %ukb %ukb %wZ\n", pspfi->TotalInUse, pspfi->PeakUsage, pspfi->TotalSize, &pspfi->PageFileName);
} while (NextEntryOffset = pspfi->NextEntryOffset);
break;
}
} while (status == STATUS_INFO_LENGTH_MISMATCH);
return status;
}
打开页面,那就很有趣了
[ZwQuerySystemInformation 不再可用于 Windows 8 。而是使用此中列出的备用功能 主题。]
但这是直接谎言 - ZwQuerySystemInformation
可用,在win 8中,赢得8.1,赢得10 ..并且在用户模式 Zw和Nt函数 - 这是别名 - 它始终指向到同一地址(这是具有2个名字的单一功能)