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.

More information

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:

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:

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 +hpcgflags /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:

The PageHeapFlags setting

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_SERIALIZEflag

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.