C++ 如何在C++中获取Windows下的内存使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/282194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get memory usage under Windows in C++
提问by Brian Stewart
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.
我试图从程序本身中找出我的应用程序消耗了多少内存。我要查找的内存使用情况是 Windows 任务管理器进程选项卡上的“内存使用情况”列中报告的数字。
采纳答案by Charlie
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.
一个好的起点是GetProcessMemoryInfo,它报告有关指定进程的各种内存信息。您可以GetCurrentProcess()
作为进程句柄传递以获取有关调用进程的信息。
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.
可能该WorkingSetSize
成员PROCESS_MEMORY_COUNTERS
是与任务管理器中的 Mem Usage coulmn 最接近的匹配项,但它不会完全相同。我会尝试使用不同的值来找到最接近您需求的值。
回答by Ronin
I think this is what you were looking for:
我想这就是你要找的:
#include<windows.h>
#include<stdio.h>
#include<tchar.h>
// Use to convert bytes to MB
#define DIV 1048576
// Use to convert bytes to MB
//#define DIV 1024
// Specify the width of the field in which to print the numbers.
// The asterisk in the format specifier "%*I64d" takes an integer
// argument and uses it to pad and right justify the number.
#define WIDTH 7
void _tmain()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
_tprintf (TEXT("There is %*ld percent of memory in use.\n"),WIDTH, statex.dwMemoryLoad);
_tprintf (TEXT("There are %*I64d total Mbytes of physical memory.\n"),WIDTH,statex.ullTotalPhys/DIV);
_tprintf (TEXT("There are %*I64d free Mbytes of physical memory.\n"),WIDTH, statex.ullAvailPhys/DIV);
_tprintf (TEXT("There are %*I64d total Mbytes of paging file.\n"),WIDTH, statex.ullTotalPageFile/DIV);
_tprintf (TEXT("There are %*I64d free Mbytes of paging file.\n"),WIDTH, statex.ullAvailPageFile/DIV);
_tprintf (TEXT("There are %*I64d total Mbytes of virtual memory.\n"),WIDTH, statex.ullTotalVirtual/DIV);
_tprintf (TEXT("There are %*I64d free Mbytes of virtual memory.\n"),WIDTH, statex.ullAvailVirtual/DIV);
_tprintf (TEXT("There are %*I64d free Mbytes of extended memory.\n"),WIDTH, statex.ullAvailExtendedVirtual/DIV);
}
回答by Ronin
GetProcessMemoryInfo is the function you are looking for. The docs on MSDN will point you in the right direction. Get the info you want out of the PROCESS_MEMORY_COUNTERS structure you pass in.
GetProcessMemoryInfo 是您正在寻找的函数。MSDN 上的文档将为您指明正确的方向。从您传入的 PROCESS_MEMORY_COUNTERS 结构中获取您想要的信息。
You'll need to include psapi.h.
您需要包含 psapi.h。
回答by Mark Ransom
Try having a look at GetProcessMemoryInfo. I haven't used it, but it looks like what you need.
尝试查看GetProcessMemoryInfo。我还没有使用它,但它看起来像你需要的。
回答by Aris Koning
To complement the answer by Ronin, indead the function GlobalMemoryStatusEx
gives you the proper counters to derive the virtual memory usage for the calling process: just substract ullAvailVirtual
from ullTotalVirtual
to get allocated virtual memory. You can check this for your self using ProcessExplorer or something.
为了配合由罗宁的回答,indead功能GlobalMemoryStatusEx
为您提供了正确的计数器导出的虚拟内存使用情况调用过程:刚。减去ullAvailVirtual
从ullTotalVirtual
获得分配的虚拟内存。您可以使用 ProcessExplorer 或其他工具自行检查。
It is confusing that the system call GlobalMemoryStatusEx
unfortunately has mixed purpose: it provides both system wide and process specific information, e.g. virtual memory information.
GlobalMemoryStatusEx
不幸的是,系统调用具有混合用途,这一点令人困惑:它提供系统范围和进程特定的信息,例如虚拟内存信息。