最近需要查看代码允许过程中内存占用情况,这里利用Windows API获取当前进程占用内存情况,另外也可以借助Intel VTune Profiler
工具(更加方便)和Visual Studio一起配合使用,便于查看程序运行时的热点、耗时等。
Windows API代码
#include<psapi.h>
void showMemoryInfo() {
HANDLE handle = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));
std::string strs;
LOG_PRINT("memory used %d M/ %d M + usage %d M/ %d M", pmc.WorkingSetSize / 1024 / 1024, pmc.PeakWorkingSetSize / 1024/1024, pmc.PagefileUsage / 1024 / 1024, pmc.PeakPagefileUsage / 1024 / 1024);
}