Windows 获取内存 API 汇总及使用方法

Windows 获取内存 API 汇总及使用方法

本文示例代码:https://gitee.com/langshanglibie/windows-memory-api
运行结果

一、获取系统内存(物理内存、虚拟内存)

GlobalMemoryStatusEx

MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex); // GlobalMemoryStatusEx 只能针对当前进程调用,不能针对指定进程调用,因为没法传递进程句柄

printf("%*ld%% - 物理内存使用率\n", WIDTH, statex.dwMemoryLoad);

printf("\n");

printf("%*.2f GB - 物理内存总量\n", WIDTH, statex.ullTotalPhys / GB);
printf("%*.2f GB - 已经使用的物理内存总量\n", WIDTH, (statex.ullTotalPhys - statex.ullAvailPhys) / GB);
printf("%*.2f GB - 剩余物理内存总量\n", WIDTH, statex.ullAvailPhys / GB);

printf("\n");

printf("%*.2f GB - 可提交最大内存总量(物理内存 + pagefile)\n", WIDTH, statex.ullTotalPageFile / GB); // 等同于 PERFORMANCE_INFORMATION.CommitLimit
printf("%*.2f GB - 已经提交内存总量(物理内存 + pagefile)\n", WIDTH, (statex.ullTotalPageFile - statex.ullAvailPageFile) / GB);
printf("%*.2f GB - 剩余内存总量(物理内存 + pagefile)\n", WIDTH, statex.ullAvailPageFile / GB); // 等同于 PERFORMANCE_INFORMATION.CommitTotal

printf("\n");

printf("%*.2f GB - 当前进程用户模式虚拟内存总量\n", WIDTH, statex.ullTotalVirtual / GB);
printf("%*.2f MB - 当前进程已经使用的用户模式虚拟内存总量\n", WIDTH, (statex.ullTotalVirtual - statex.ullAvailVirtual) / MB);
printf("%*.2f GB - 当前进程剩余用户模式虚拟内存总量\n", WIDTH, statex.ullAvailVirtual / GB);

printf("\n");

printf("%*.2f GB - AvailExtendedVirtual(Reserved, is always 0)\n", WIDTH, statex.ullAvailExtendedVirtual / GB);

GetPhysicallyInstalledSystemMemory

The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer’s SMBIOS firmware tables. This can differ from the amount reported by the GlobalMemoryStatusEx function, which sets the ullTotalPhys member of the MEMORYSTATUSEX structure to the amount of physical memory that is available for the operating system to use. The amount of memory available to the operating system can be less than the amount of memory physically installed in the computer because the BIOS and some drivers may reserve memory as I/O regions for memory-mapped devices, making the memory unavailable to the operating system and applications.
译文:
GetPhysicalyInstalledSystemMemory函数从计算机的SMBIOS固件表中检索物理安装的RAM数量。这可能与GlobalMemoryStatusEx函数报告的数量不同,该函数将MEMORYSTATUSEX结构的ullTotalPhys成员设置为操作系统可使用的物理内存量。操作系统可用的内存量可能小于物理安装在计算机中的内存量,因为BIOS和一些驱动程序可能会将内存保留为内存映射设备的I/O区域,从而使内存对操作系统和应用程序不可用。

ULONGLONG totalMemoryInKilobytes = 0;
GetPhysicallyInstalledSystemMemory(&totalMemoryInKilobytes);
printf("%*.2f GB - 物理内存总量(电脑安装的)\n", WIDTH, totalMemoryInKilobytes / MB);

GetPerformanceInfo

PERFORMANCE_INFORMATION pi = {sizeof(PERFORMANCE_INFORMATION)};
::GetPerformanceInfo(&pi, sizeof(PERFORMANCE_INFORMATION));
// 对应任务管理器【性能】->【已提交】
printf("%*.2f GB - 可提交最大内存总量(物理内存 + pagefile)\n", WIDTH, 1.0 * pi.CommitLimit * pi.PageSize / GB); // 乘以 1.0 换为 double,否则数值越界被截断!
printf("%*.2f GB - 已经提交内存总量(物理内存 + pagefile)\n", WIDTH, 1.0 * pi.CommitTotal * pi.PageSize / GB);
printf("%*.2f GB - 剩余内存总量(物理内存 + pagefile)\n", WIDTH, 1.0 * (pi.CommitLimit - pi.CommitTotal) * pi.PageSize / GB);

printf("\n");

printf("%*.2f GB - 物理内存总量\n", WIDTH, 1.0 * pi.PhysicalTotal * pi.PageSize / GB);
printf("%*.2f GB - 已经使用的物理内存总量\n", WIDTH, 1.0 * (pi.PhysicalTotal - pi.PhysicalAvailable) * pi.PageSize / GB);
printf("%*.2f GB - 剩余物理内存总量\n", WIDTH, 1.0 * pi.PhysicalAvailable * pi.PageSize / GB);

printf("\n");

printf("%*d - 系统中当前句柄数\n", WIDTH, pi.HandleCount);
printf("%*d - 系统中当前进程数\n", WIDTH, pi.ProcessCount);
printf("%*d - 系统中当前线程数\n", WIDTH, pi.ThreadCount);

二、获取进程内存(物理内存、虚拟内存)

GetProcessMemoryInfo

PROCESS_MEMORY_COUNTERS_EX pmc;
pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
if (GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc)))
{
    printf("%*d - 发生页错误次数\n", WIDTH, pmc.PageFaultCount);
    printf("%*.2f MB - 指定进程工作集内存峰值\n", WIDTH, pmc.PeakWorkingSetSize / MB);
    printf("%*.2f MB - 指定进程工作集内存\n", WIDTH, pmc.WorkingSetSize / MB);

    printf("\n");

    printf("%*.2f MB - 指定进程已经提交的私有内存总量(PagefileUsage)\n", WIDTH, pmc.PagefileUsage / MB);
    printf("%*.2f MB - 指定进程已经提交的私有内存总量峰值 PeakPagefileUsage\n", WIDTH, pmc.PeakPagefileUsage / MB);
    printf("%*.2f MB - 指定进程已经提交的私有内存总量(PrivateUsage)\n", WIDTH, pmc.PrivateUsage / MB);
}

QueryWorkingSet

这个函数使用有点复杂,可以参考:

GetProcessWorkingSetSize

Retrieves the minimum and maximum working set sizes of the specified process.
对于获取系统或进程内存没什么用

参考

  • 17
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值