Dumping Memory Statistics

The CMemoryState member function Difference will determine the difference between two memory-state objects. It will detect any objects that were not deallocated from the heap between the beginning and end memory-state snapshots.

·To dump memory statistics:

The following example, continuing the example from the previous section, shows how to call DumpStatistics to get information about the objects that have not been deallocated.

if( diffMemState.Difference( oldMemState, newMemState ) )

{

TRACE( "Memory leaked !\n" );

diffMemState.DumpStatistics();

}

A sample dump from the example above is shown here:

0 bytes in 0 Free Blocks

22 bytes in 1 Object Blocks

45 bytes in 4 Non-Object Blocks

Largest number used: 67 bytes

Total allocations: 67 bytes

The first line describes the number of blocks whose deallocation was delayed if afxMemDF was set to delayFreeMemDF. For a description of afxMemDF, see the section “To select specific memory diagnostic features with afxMemDF” on page 291.

The second line describes how many objects still remain allocated on the heap.

The third line describes how many nonobject blocks (arrays or structures allocated with new) were allocated on the heap and not deallocated.

The fourth line gives the maximum memory used by your program at any one time.

The last line lists the total amount of memory used by your program.