Heap fragmentationIn heap-based memory allocation, memory is allocated from a large pool of unused memory. This pool is called the heap.
When the available memory is broken into small, noncontiguous blocks, this is known as heap fragmentation. When heap fragmentation occurs, memory allocation may fail, even though there is sufficient memory in the heap to satisfy the request. This behavior occurs because no one block of memory is large enough to satisfy the allocation request. For programs that have low memory requirements, the standard heap is sufficient. Typically, heap fragmentation does not cause allocation failure. However, if the program allocates memory frequently, and if the program uses a variety of allocation sizes, memory allocation may fail because of heap fragmentation.
Low fragmentation heapThe low fragmentation heap mechanism (LFH) was introduced in Windows 2000, and it is included in Windows XP and in Windows Server 2003. Although this mechanism is built on the existing heap, it successfully reduces fragmentation of heap memory.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
Why you cannot enable the LFHYou cannot enable the LFH if you are using certain global flags (gflags) that are related to the heap. You can set these gflags by using the Gflags tool that is included in many versions of Windows and that is also included in the Windows debugging tools. For more information about these debugging tools, visit the following Microsoft Web site:For more information, click the following article number to view the article in the Microsoft Knowledge Base:
Global flags that can disable LFHAlthough global flags are used in debugging, you may unintentionally disable the LFH when you use them. You can use the Gflags tool to enable different global flags for a program. You can run the Gflags tool from the command line, or you can use the graphical user interface (GUI) dialog box to run the Gflags tool. When you use the GUI dialog box, you must set the different global flag options on the Image File tab in the Gflags tool. For more information about command-line options, visit the following Microsoft TechNet Web site:
gflags /i notepad.exe –hpc
The first command installs the global flags, and the second command uninstalls the global flags. In these commands, "hpc" means "enable heap parameter checking." When you enable a global flag, you create a hexadecimal value for the program's subkey under the following registry subkey:
Value type: DWORD
Value data: 0x00000040
|Gflag||Value data||Affects LFH?|
|Stop on exception||00000001||No|
|Disable stack extension||00010000||No|
|Show loader snaps||00000002||No|
|Enable heap tail checking||00000010||Yes|
|Enable system critical breaks||100000||No|
|Enable heap free checking||00000020||Yes|
|Disable heap coalesce on free||200000||No|
|Enable heap parameter checking||00000040||Yes|
|Enable heap validation on call||00000080||Yes|
|Enable application verifier||00000100||No|
|Enable page heap||02000000||Yes|
|Enable heap tagging||00000800||Yes|
|Create user mode stack trace database||00001000||No|
|Early critical section event creation||10000000||No|
|Enable heap tagging by DLL||00008000||No|
|Disable protected DLL verification||80000000||No|
The PageHeapFlags settingThe PageHeapFlags registry entry is located separately under the following registry subkey:
When you use the Gflags tool to enable the page heap, the value of the PageHeapFlags registry entry is set to 0x00000003. Clearing the Enable page heap check box and applying the changes does not delete or change the value of the PageHeapFlags registry entry. Instead, clearing this check box sets the GlobalFlag value for the page heap to 0. Setting this value to 0 overrides and disables the behavior that is configured by using the PageHeapFlags registry entry. Alternatively, you can delete the PageHeapFlags registry entry or manually change the value to 0 without using the Gflags tool.
Other flags that can disable LFHAdditionally, the following gflags can also disable LFH.
The HEAP_NO_SERIALIZE flagBy default, the HEAP_NO_SERIALIZE flag to the API HeapAlloc() is not set. When heap access is serialized, two or more threads can allocate memory and can free memory from the same heap at the same time. If you set this flag, LFH is disabled. When this flag is set, it means that mutual exclusion will not be used when the heap functions allocate free memory from this heap.
The HEAP_GROWABLE flagWhen the HEAP_GROWABLE flag is not set as a flag to RtlCreateHeal(), LFH is disabled. When the heap cannot be extended, there may be insufficient space in the heap for the application because of the large footprint of LFH. By setting this flag, you enable the heap to grow, as necessary. You must specify this flag if the HeapBase address is NULL.
How to prevent debuggers from disabling LFHWhen you use a debugger, LFH may not work as expected. This behavior may occur because of several debug options. By default, these debug options are frequently enabled. To avoid this problem, use one of the following methods.
Method 1When you use the WinDbg debugger or the Cdb debugger, you can use the following command to disable heap debugging options:
Method 2You can prevent the WinDbg debugger or the Cdb debugger from disabling LFH by using the following command to set an environment variable:
Article ID: 929136 - Last Review: Oct 25, 2012 - Revision: 1