frust99
frust99

Reputation:

Remaining heap size

I know this might be an oversimplification - but I need to know the amount of free memory I could allocate in my program. It's a windows mobile project (c++) and it seems like I might have a memory leak hiding somewhere. Calling a function which returns the remaining size (or remaining chunks) of heap storage I could access would be perfect. Is there some way to traverse the available chunks of memory the operator new uses? Or a built-in WINAPI (or winmobile) function that does it?

Upvotes: 1

Views: 3362

Answers (3)

SteveS
SteveS

Reputation: 514

Have a look at the heap state reporting functions:

http://msdn.microsoft.com/en-us/library/wc28wkas.aspx

First, include crtdbg:

    #include <crtdbg.h>

then in your code:

_CrtMemState ms;
_CrtMemCheckpoint(&ms);
_CrtMemDumpStatistics(&ms);

this will output like so to the debug output window:

      0 bytes in 0 Free Blocks.
      56596 bytes in 2056 Normal Blocks.
      17895 bytes in 83 CRT Blocks.
      0 bytes in 0 Ignore Blocks.
      0 bytes in 0 Client Blocks.
      Largest number used: 74491 bytes.
      Total allocations: 240054 bytes.

The default heap reserved space is 1MB, this can be adjusted in the Project Properties, Configuration Properties, Linker, System, "Heap Reserve Size" (VS 2010)

You might also want to look at _CrtSetDbgFlag http://msdn.microsoft.com/en-us/library/5at7yxcs(v=VS.100).aspx

Upvotes: 0

avakar
avakar

Reputation: 32685

Check out GlobalMemoryStatus. It will provide the amount of free physical memory. Note that older Windows CE enforce 32MB per process limit (Windows CE 6.0 lifts this limit).

Upvotes: 1

Related Questions