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
- PROCESS_MEMORY_COUNTERS
- PROCESS_MEMORY_COUNTERS_EX
- PROCESS_MEMORY_COUNTERS_EX2:Windows 10 22H2 with September 2023 cumulative update or Windows 11 22H2 with September 2023 cumulative update
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
这个函数使用有点复杂,可以参考:
- chrome base 库中 process_metrics_win.cc 文件中 GetWorkingSetKBytes 函数有完整的使用方法,但是发现最新版中已经没有此函数了
- python - 获取与 Windows 中的任务管理器相同的进程详细信息
- Getting same process details as task manager in Windows
GetProcessWorkingSetSize
Retrieves the minimum and maximum working set sizes of the specified process.
对于获取系统或进程内存没什么用