Heap fragmentation
In 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 heap
The 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:
816542 The Windows XP low fragmentation heap algorithm feature is available for Windows 2000
If programs allocate large amounts of memory in various allocation sizes, it is optimal if they use LFH. LFH allocates blocks of memory that are as large as 16 kilobytes (KB). For memory blocks that are larger than 16 KB, the LFH uses the standard heap. For more information, visit the following Microsoft Web site:
The LFH algorithm minimizes heap fragmentation. Additionally, the LFH algorithm improves Win32 heap allocation performance. This algorithm works by allocating variable-length memory blocks or by deallocating variable-length memory blocks. This algorithm also scales well on multiprocessor computers.
Why you cannot enable the LFH
You 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:
243043 How to set the GlobalFlag registry value using the Gflags.exe tool
Global flags that can disable LFH
Although 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:
For example, you can use the following commands at a command prompt to install the global flags or to uninstall the global flags for the Notepad program:
gflags /i notepad.exe +hpc
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:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
For example, when you set the global flag for a program, a registry entry that resembles the following is created:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<Myprogram.exe>
Name: GlobalFlag
Value type: DWORD
Value data: 0x00000040
You can set the following options on the
Image File tab in the Gflags tool.
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 |
Note If you set more than one gflag, the value of the registry entry will be the sum of the values of all the gflags that you set.
The PageHeapFlags setting
The PageHeapFlags registry entry is located separately under the following registry subkey:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<My_Program.exe>
The PageHeapFlags registry entry is used together with the GlobalFlag registry entry values. Unless you are engaged in debugging or tracing a problem with the help of Microsoft Customer Support Services, we recommended that you keep the PageHeapFlags value set to 0 at all times.
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 LFH
Additionally, the following gflags can also disable LFH.
The HEAP_NO_SERIALIZE flag
By 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 flag
When 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 LFH
When 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 1
When you use the WinDbg debugger or the Cdb debugger, you can use the following command to disable heap debugging options:
-hd
Method 2
You can prevent the WinDbg debugger or the Cdb debugger from disabling LFH by using the following command to set an environment variable:
Set NO_DEBUG_HEAP=1
You can also use the following setting for the image. However, we do not recommend this setting as highly.
Set NtGlobalFlags = 0