c++ - How to verify memory usage -


i'd implement c++ function (using win32 api) current memory usage of process.

i checked code post how determine cpu , memory consumption inside process? , implemented function.

bool getmemoryusage( size_t& usageinbytes ) {     process_memory_counters_ex pmc;     if ( getprocessmemoryinfo(getcurrentprocess(), (process_memory_counters*) &pmc, sizeof(pmc)) )     {         usageinbytes = pmc.workingsetsize;         return true;     }     else     {         return false;     } } 

however, when try test that:

size_t initialmemoryusage = 0; getmemoryusage( initialmemoryusage );  size_t use = 596; void* memused = malloc( use );  size_t memoryusage = 0; getmemoryusage( memoryusage ); assert( memoryusage == initialmemoryusage + 596 );  free( memused );  getmemoryusage( memoryusage ); assert( memoryusage == initialmemoryusage ); 

it fails:

  • if getmemoryusage uses pmc.workingsetsize, memoryusage greater initialmemoryusage 12288 , freeing memory not make decrease
  • if getmemoryusage uses pmc.privateusage, memoryusage , initialmemoryusage equal, after allocated memory

how can implement getmemoryusage in accurate , reliable way have test above pass?

actually, want check @ runtime specific function call not introduce memory leak. want check memory allocated function released. need know @ point how many memory allocated new/malloc , not released yet.

sorry, doesn't work way.

 std::string foo()  {      return "hello";  } 

when function returns, there may new std::string didn't exist before. doesn't indicate leak.

you have expectations how software works isn't how software works.

i think find more fruitful @ how programmers typically debug memory leaks. common mistake people make when asking solving problem come way think problem should solved , ask getting way work. that's you're doing.

instead, describe actual problem in detail possible , ask experts suggest how think should solve it. they're more suggest ways work.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -