windows系统资源的获取函数主要有2个:GetPerformanceInfo 和 GlobalMemoryStatusEx 定义如下:
<pre name="code" class="cpp"> typedef struct _PERFORMANCE_INFORMATION {
DWORD cb; // 结构体数据大小 sizeof(_PERFORMANCE_INFORMATION)
SIZE_T 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; // 页大小(单位:字节)
DWORD HandleCount;
DWORD ProcessCount;
DWORD ThreadCount;
} PERFORMANCE_INFORMATION, *PPERFORMANCE_INFORMATION, PERFORMACE_INFORMATION, *PPERFORMACE_INFORMATION;
BOOL
WINAPI
GetPerformanceInfo (
PPERFORMACE_INFORMATION pPerformanceInformation,
DWORD cb
);
typedef struct _MEMORYSTATUSEX {
DWORD dwLength; // 结构体数据大小 sizeof(_MEMORYSTATUSEX)
DWORD dwMemoryLoad; // 内存使用率
DWORDLONG ullTotalPhys; // 物理内存总大小(单位:字节)
DWORDLONG ullAvailPhys; // 物理内存可用大小(单位:字节)
DWORDLONG ullTotalPageFile; // 总页面大小(物理内存总大小+硬盘划分的虚拟内存总大小)(单位:字节)
DWORDLONG ullAvailPageFile; // 当前进程能获取的最大内存数(单位:字节)
DWORDLONG ullTotalVirtual; // 当前进程最大内存寻址地址(默认2GB,开启大地址后就是4GB)
DWORDLONG ullAvailVirtual; // 当前进程可用最大内存(单位:字节)
DWORDLONG ullAvailExtendedVirtual;
} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;
WINBASEAPI
BOOL
WINAPI
GlobalMemoryStatusEx(
__out LPMEMORYSTATUSEX lpBuffer
);
两个接口有些数据是相同的,有些是有相互关系的,这个在MSDN上可以查到。另外GetPerformanceInfo 这个接口需要调用Psapi库,简单的测试代码如下:
</pre><p><pre name="code" class="cpp">#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include "Psapi.h"
#pragma comment(lib, "Psapi.Lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "GetPerformanceInfo接口测试:" << endl;
int nMB = 1024 * 1024;
//ProcessQUERY(5);
PERFORMANCE_INFORMATION stPerformance = {0};
int cb = sizeof(stPerformance);
GetPerformanceInfo(&stPerformance, cb);
int nPageSize = stPerformance.PageSize;
INT64 i64PhyTotal = (INT64)stPerformance.PhysicalTotal * nPageSize / nMB;
INT64 i64PhyAvail = (INT64)stPerformance.PhysicalAvailable * nPageSize / nMB;
cout << "Commit Total:" << (INT64)stPerformance.CommitTotal * nPageSize / nMB << endl;
cout << "Commit Limit:" << (INT64)stPerformance.CommitLimit * nPageSize / nMB << "MB\t" << "Commit peak:\t"
<span style="white-space:pre"> </span> << (INT64)stPerformance.CommitPeak * nPageSize / nMB << "MB\t" << endl;
cout << "物理内存:\t" << "总量:\t" << i64PhyTotal << "MB\t" << "剩余量:\t" << i64PhyAvail << "MB\t" << endl;
cout << "System Cache:" << (INT64)stPerformance.SystemCache * nPageSize / (1024 * 1024) << "MB\t" << endl;
cout << "Kernel Total:" << (INT64)stPerformance.KernelTotal * nPageSize / (1024 * 1024) << "MB\t" << "Kernel paged:\t"
<span style="white-space:pre"> </span> << (INT64)stPerformance.KernelPaged * nPageSize / (1024 * 1024) << "MB\t"
<< "Kernel Nopaged:" << (INT64)stPerformance.KernelNonpaged * nPageSize / nMB << "MB" << endl;
cout << "Page Size:" << stPerformance.PageSize << "\t" << endl;
cout << "Handle count:" << stPerformance.HandleCount << "\t" << endl;
cout << "Process count:" << stPerformance.ProcessCount << "\t" << endl;
cout << "Thread count:" << stPerformance.ThreadCount << "\t" << endl;
cout << "GlobalMemoryStatusEx接口测试:" << endl;
MEMORYSTATUSEX stMem = {0};
stMem.dwLength = sizeof(stMem);
GlobalMemoryStatusEx(&stMem);
cout << "内存使用率:" << stMem.dwMemoryLoad << '%' << endl;
cout << "物理内存:\t" << "总量:\t" << stMem.ullTotalPhys / (1024 * 1024) << "MB\t" << "剩余量:\t"
<span style="white-space:pre"> </span><< stMem.ullAvailPhys / (1024 * 1024) << "MB\t" << endl;
cout << "页面文件:\t" << "总量:\t" << stMem.ullTotalPageFile / (1024 * 1024) << "MB\t" << "剩余量:\t"
<span style="white-space:pre"> </span><< stMem.ullAvailPageFile / (1024 * 1024) << "MB\t" << endl;
cout << "虚拟内存:\t" << "总量:\t" << stMem.ullTotalVirtual / (1024 * 1024) << "MB\t" << "剩余量:\t"
<span style="white-space:pre"> </span><< stMem.ullAvailVirtual / (1024 * 1024) << "MB\t" << endl;
return 0;
}
测试结果如下: