2016年7月14日 星期四

使用 Windows API 顯示記憶體用量

#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


2016年6月6日 星期一

Remove newline from std::string by "remove" function only is not working?

Test :

calling following:

remove(strRet.begin(), strRet.end(), '\n');

strRet is a std::string containing newline


It's not working!!!!!!!!!!!
if someone print it out....


Reson:

The function cannot alter the properties of the object containing the range of elements (i.e., it cannot alter the size of an array or a container)

Solution:

strRet.erase(remove(strRet.begin(), strRet.end(), '\n'), strRet.end());  

1. Calling remove and get a new end of std::string
2. Erase the removed part