Knowledge Base Nr: 00169 memtrace.cpp - http://www.swe-kaiser.de

Win32: globalen memory status abfragen und tracen
(nützlich zum finden von memory leaks die vom visual studio nicht gefunden werden)

  
void TRACEMEM2(const char* lpszText, bool bAll)
{
#ifdef _DEBUG
const int DIV = 1024;
char *divisor = "K";
const int WIDTH = 10;

MEMORYSTATUS stat;

GlobalMemoryStatus (&stat);

DWORD dwFree = stat.dwAvailPhys + stat.dwAvailPageFile;

TRACE ("\n\nMEMTRACE: %s\nfree mem: %*ld free %sbytes\n", lpszText, WIDTH, dwFree/DIV, divisor);
if(bAll)
{
TRACE ("\nThe MemoryStatus structure is %ld bytes long.\n",
stat.dwLength);
TRACE ("It should be %d.\n", sizeof (stat));
TRACE ("%ld percent of memory is in use.\n",
stat.dwMemoryLoad);
TRACE ("There are %*ld total %sbytes of physical memory.\n",
WIDTH, stat.dwTotalPhys/DIV, divisor);
TRACE ("There are %*ld free %sbytes of physical memory.\n",
WIDTH, stat.dwAvailPhys/DIV, divisor);
TRACE ("There are %*ld total %sbytes of paging file.\n",
WIDTH, stat.dwTotalPageFile/DIV, divisor);
TRACE ("There are %*ld free %sbytes of paging file.\n",
WIDTH, stat.dwAvailPageFile/DIV, divisor);
TRACE ("There are %*lx total %sbytes of virtual memory.\n",
WIDTH, stat.dwTotalVirtual/DIV, divisor);
TRACE ("There are %*lx free %sbytes of virtual memory.\n",
WIDTH, stat.dwAvailVirtual/DIV, divisor);
}
#endif
}