我试图找出我的应用程序在程序本身内消耗了多少内存.我正在寻找的内存使用量是Windows任务管理器的"进程"选项卡上"内存使用情况"列中报告的数量.
一个很好的起点是GetProcessMemoryInfo,它报告有关指定进程的各种内存信息.您可以GetCurrentProcess()
作为进程句柄传递,以获取有关调用进程的信息.
可能是任务管理器中与Mem Usage coulmn最接近的WorkingSetSize
成员PROCESS_MEMORY_COUNTERS
,但它不会完全相同.我会尝试不同的值来找到最符合您需求的值.
我认为这就是你要找的东西:
#include#include #include // 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); }
GetProcessMemoryInfo是您正在寻找的功能.MSDN上的文档将指出您正确的方向.从传入的PROCESS_MEMORY_COUNTERS结构中获取所需的信息.
你需要包含psapi.h.
试试看GetProcessMemoryInfo.我没有使用它,但它看起来像你需要的.