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("%*.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("%*.2f GB - PageFile 内存总量\n", WIDTH, statex.ullTotalPageFile / GB);
printf("%*.2f GB - 已经使用的 PageFile 内存总量\n", WIDTH, (statex.ullTotalPageFile - statex.ullAvailPageFile) / GB);
printf("%*.2f GB - 剩余 PageFile 内存总量\n", WIDTH, statex.ullAvailPageFile / GB);

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("%*.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

printf("%*.2f GB - 系统可提交最大内存总量\n", WIDTH, 1.0 * pi.CommitLimit * pi.PageSize / GB); // 乘以 1.0 换为 double,否则数值越界被截断!
printf("%*.2f GB - 系统已经提交内存总量\n", WIDTH, 1.0 * pi.CommitTotal * pi.PageSize / GB);
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("%*d - 系统中当前句柄数\n", WIDTH, pi.HandleCount);
printf("%*d - 系统中当前进程数\n", WIDTH, pi.ProcessCount);
printf("%*d - 系统中当前线程数\n", WIDTH, pi.ThreadCount);

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

GetProcessMemoryInfo

printf("%*d - 发生页错误次数\n", WIDTH, pmc.PageFaultCount);
printf("%*.2f MB - 指定进程工作集内存峰值\n", WIDTH, pmc.PeakWorkingSetSize / MB);
printf("%*.2f MB - 指定进程工作集内存\n", WIDTH, pmc.WorkingSetSize / MB);
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.
对于获取系统或进程内存没什么用

参考

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 19:04:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 19:04:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 19:04:02       87 阅读
  4. Python语言-面向对象

    2024-02-19 19:04:02       96 阅读

热门阅读

  1. logback实践

    2024-02-19 19:04:02       42 阅读
  2. 小程序API能力汇总——基础容器API(一)

    2024-02-19 19:04:02       45 阅读
  3. inline内联函数为什么不能是虚函数?

    2024-02-19 19:04:02       48 阅读
  4. Redis的持久化机制

    2024-02-19 19:04:02       59 阅读
  5. P1030 [NOIP2001 普及组] 求先序排列

    2024-02-19 19:04:02       51 阅读
  6. flutter 功能

    2024-02-19 19:04:02       47 阅读