Configuring the Memory Manager

From RAD Studio
Jump to: navigation, search

Go Up to Managing Memory Index


This section describes how to configure the Memory Manager on 32-bit Windows and 64-bit Windows. Other platforms use the Posix/32 memory manager, which does not support this functionality.

Note: You can change some memory manager configuration settings while the Memory Manager is in use. All the configuration settings are global settings and affect all threads that are using the Memory Manager. Unless otherwise stated, all functions and procedures are thread safe. These configuration options are for the local Memory Manager only. Setting these options inside a library when the library is sharing the Memory Manager of the main application will have no effect.

To set the minimum block alignment for the Memory Manager

  1. Use the function GetMinimumBlockAlignment to fetch the current minimum block alignment.
  2. Select the appropriate memory block alignment for your application. Available block alignments are 8-byte (System.mba8Byte) and 16-byte (System.mba16Byte).
  3. To change the memory block alignment, use the procedure SetMinimumBlockAlignment.

Note: Memory allocated through the Memory Manager is guaranteed to be aligned to at least 8-byte boundaries. 16-byte alignment is useful when memory blocks will be manipulated using SSE instructions, but may increase the memory usage overhead.

To report memory leaks on shutdown

  1. Set the global variable System.ReportMemoryLeaksOnShutdown to True.
  2. When the Memory Manager shuts down, it scans the memory pool and report all unregistered memory leaks in a message dialog. To register and unregister expected memory leaks, use the RegisterExpectedMemoryLeak and UnregisterExpectedMemoryLeak procedures.

Note: The Memory Manager can report memory that was allocated but not freed at the time the Memory Manager shuts down. Such memory blocks are called memory leaks and are often the result of programming errors. The default value for System.ReportMemoryLeaksOnShutdown is False. The class of a leak is determined by examining the first dword in the block. The reported classes of leaks may not always be 100% accurate. A leak is reported as a string leak if it appears to be an AnsiString. If the Memory Manager is unable to estimate the type of leak, it will be reported as belonging to the unknown class.

To never sleep on thread contention in the Memory Manager

  1. Set the global variable System.NeverSleepOnMMThreadContention to True.
  2. When a thread contention occurs inside the Memory Manager, it will wait inside a loop until the contention is resolved.

Note: The Memory Manager is a shared resource, and when many threads in the application attempt to perform a Memory Manager operation at the same time, one or more threads might have to wait for a pending operation in another thread to complete before it can continue. This situation is called thread contention. When a thread contention occurs inside the Memory Manager, the default behavior is to relinquish the remaining time in the thread's time slice. If the resource is still not available when the thread enters its next time slice, the Memory Manager calls the OS procedure Sleep to force it to wait longer (roughly 20 milliseconds) before trying again. This behavior works well on machines with single or dual core CPUs, and also when the ratio of the number of running threads to number of CPU cores is relatively high (greater than 2:1). In other situations, better performance can be obtained by entering a busy waiting loop until the resource becomes available. If System.NeverSleepOnMMThreadContention is True, the Memory Manager will enter a wait loop instead of scheduling out. The default value for System.NeverSleepOnMMThreadContention is False.

Getting the Source for the Memory Manager

Full source code for the Memory Manager (FastMM) is available on .

With the full version of FastMM, you can run the memory manager in a special "debug" mode which is useful for detecting heap corruption and memory leaks. Additional features in the full version of FastMM:

  • Double free objects / interfaces
  • File logging and reports

For more information, please see the comments in the FastMM source code (FastMM4.pas and FastMM4Options.inc).

See Also