如何在C++中获取Windows下的内存使用情况

2021-12-24 00:00:00 windows memory-management c++

我试图从程序本身中找出我的应用程序消耗了多少内存.我要查找的内存使用情况是 Windows 任务管理器进程"选项卡上内存使用情况"列中报告的数字.

I am trying to find out how much memory my application is consuming from within the program itself. The memory usage I am looking for is the number reported in the "Mem Usage" column on the Processes tab of Windows Task Manager.

推荐答案

一个好的起点是 GetProcessMemoryInfo,报告有关指定进程的各种内存信息.您可以将 GetCurrentProcess() 作为进程句柄传递,以获取有关调用进程的信息.

A good starting point would be GetProcessMemoryInfo, which reports various memory info about the specified process. You can pass GetCurrentProcess() as the process handle in order to get information about the calling process.

可能 PROCESS_MEMORY_COUNTERSWorkingSetSize 成员与任务管理器中的 Mem Usage coulmn 最接近,但它不会完全相同.我会尝试使用不同的值来找到最接近您需求的值.

Probably the WorkingSetSize member of PROCESS_MEMORY_COUNTERS is the closest match to the Mem Usage coulmn in task manager, but it's not going to be exactly the same. I would experiment with the different values to find the one that's closest to your needs.

相关文章