最近需要查看代碼允許過程中內存占用情況,這里利用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);
}
