Article ID: 929136 - View products that this article applies to.
On some computers that are running Microsoft Windows Server 2003, Microsoft Windows XP, or Microsoft Windows 2000, the low fragmentation heap (LFH) mechanism may be disabled. This article describes what causes this behavior. It also describes how you can prevent this behavior.
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:
816542If 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:
(http://support.microsoft.com/kb/816542/ )The Windows XP low fragmentation heap algorithm feature is available for Windows 2000
http://msdn2.microsoft.com/en-us/library/aa366750.aspxThe 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 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:
http://www.microsoft.com/whdc/devtools/debugging/default.mspxFor more information, click the following article number to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/243043/ )How to set the GlobalFlag registry value using the Gflags.exe tool
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:
http://technet2.microsoft.com/WindowsServer/en/library/e77bf7f8-b9a5-48a7-9223-be6fae41393c1033.mspx?mfr=trueFor 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:
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
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<Myprogram.exe>
Name: GlobalFlagYou can set the following options on the Image File tab in the Gflags tool.
Value type: DWORD
Value data: 0x00000040
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.
Collapse this tableExpand this table
The PageHeapFlags settingThe PageHeapFlags registry entry is located separately under the following registry subkey:
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.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\<My_Program.exe>
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:
Set NO_DEBUG_HEAP=1You can also use the following setting for the image. However, we do not recommend this setting as highly.
Set NtGlobalFlags = 0
Article ID: 929136 - Last Review: October 25, 2012 - Revision: 1.3