#include <Windows.h>
#include <psapi.h>
#include <sstream>
// Use to convert bytes to KB
#define KB 1024
void PrintMemoryUsage()
{
PROCESS_MEMORY_COUNTERS memory;
HANDLE hProcess = GetCurrentProcess();
/*
BOOL GetProcessMemoryInfo(
[in] HANDLE Process,
[out] PPROCESS_MEMORY_COUNTERS ppsmemCounters,
[in] DWORD cb
);
*/
std::ostringstream oss;
if (GetProcessMemoryInfo(hProcess, &memory, sizeof(memory)))
{
oss << "Working Set Size: " << memory.WorkingSetSize / KB << " KB\n";
oss << "Peak Working Set Size: " << memory.PeakWorkingSetSize / KB << " KB\n";
oss << "Pagefile Usage: " << memory.PagefileUsage / KB << " KB\n";
oss << "Peak Pagefile Usage: " << memory.PeakPagefileUsage / KB << " KB\n";
std::string s = oss.str();
MessageBoxA(0, s.c_str(), "", MB_OK);
}
CloseHandle(hProcess);
}
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
PrintMemoryUsage();
return 0;
}
/*
run:
Working Set Size: 4728 KB
Peak Working Set Size: 4732 KB
Pagefile Usage: 1072 KB
Peak Pagefile Usage: 1072 KB
*/