Microsoft KB Archive/929136

= Why the low fragmentation heap (LFH) mechanism may be disabled on some computers that are running Windows Server 2003, Windows XP, or Windows 2000 =

Article ID: 929136

Article Last Modified on 4/3/2007

-

APPLIES TO


 * Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
 * Microsoft Windows Server 2003, Standard Edition (32-bit x86)
 * Microsoft Windows XP Professional
 * Microsoft Windows XP Home Edition
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Professional Edition

-



INTRODUCTION
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 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:

http://msdn2.microsoft.com/en-us/library/aa366750.aspx

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:

http://www.microsoft.com/whdc/devtools/debugging/default.mspx

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:

http://technet2.microsoft.com/WindowsServer/en/library/e77bf7f8-b9a5-48a7-9223-be6fae41393c1033.mspx?mfr=true

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 +ust

gflags /i notepad.exe –ust'''

The first command installs the global flags, and the second command uninstalls the global flags. In these commands, &quot;ust&quot; means &quot;create user mode stack trace database.&quot; 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:

Name: GlobalFlag

Value type: DWORD

Value data: 0x00010000

You can set the following options on the Image File tab in the Gflags tool.

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:

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 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, 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

Keywords: kbexpertiseadvanced kbxplatform kbmemory kbdebug kbhowto KB929136

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.