#include "windows.h"
#include "psapi.h"
QString CvtByteToFileSize(double Bytes)
{
QString Unit;
if (Bytes < 1024) {
Unit = " bytes";
} else if (Bytes < 1024*1024) {
Bytes /= 1024;
Unit = " kB";
} else {
Bytes /= 1024*1024;
Unit = " MB";
}
return QString::number(Bytes) + Unit;
}
void PrintMemUsage()
{
/* Virtual memory */
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memInfo);
DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;
DWORDLONG virtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
LOG("Virtual Total: [%s] Used: [%s]",
CvtByteToFileSize(totalVirtualMem).toStdString().c_str(),
CvtByteToFileSize(virtualMemUsed).toStdString().c_str());
LOG("=======================");
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))){
LOG("\tPageFaultCount: %s", CvtByteToFileSize(pmc.PageFaultCount).toStdString().c_str());
LOG("\tPeakWorkingSetSize: %s", CvtByteToFileSize(pmc.PeakWorkingSetSize).toStdString().c_str());
LOG("\tWorkingSetSize: %s", CvtByteToFileSize(pmc.WorkingSetSize).toStdString().c_str());
LOG("\tQuotaPeakPagedPoolUsage: %s", CvtByteToFileSize(pmc.QuotaPeakPagedPoolUsage).toStdString().c_str());
LOG("\tQuotaPagedPoolUsage: %s", CvtByteToFileSize(pmc.QuotaPagedPoolUsage).toStdString().c_str());
LOG("\tQuotaPeakNonPagedPoolUsage: %s", CvtByteToFileSize(pmc.QuotaPeakNonPagedPoolUsage).toStdString().c_str());
LOG("\tQuotaNonPagedPoolUsage: %s", CvtByteToFileSize(pmc.QuotaNonPagedPoolUsage).toStdString().c_str());
LOG("\tPagefileUsage: %s", CvtByteToFileSize(pmc.PagefileUsage).toStdString().c_str());
LOG("\tPeakPagefileUsage: %s", CvtByteToFileSize(pmc.PeakPagefileUsage).toStdString().c_str());
}
/* Physical memory */
DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
DWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
LOG("Physical Total: [%s] Used: [%s] UsedByMe: [%s]",
CvtByteToFileSize(totalPhysMem).toStdString().c_str(),
CvtByteToFileSize(physMemUsed).toStdString().c_str(),
CvtByteToFileSize(physMemUsedByMe).toStdString().c_str());
}
|
Reference:
1. How to determine CPU and memory consumption from inside a process?http://stackoverflow.com/questions/63166/how-to-determine-cpu-and-memory-consumption-from-inside-a-process
2. PROCESS_MEMORY_COUNTERS structure
3. Collecting Memory Usage Information For a Process