Microsoft KB Archive/323635

= You may experience a C-Runtime heap performance problem in a Visual C++ application that is running on Windows 2000 or on Windows XP =

Article ID: 323635

Article Last Modified on 1/6/2006

-

APPLIES TO


 * Microsoft Windows XP Professional
 * Microsoft Windows 2000 Standard Edition
 * Microsoft Windows NT 4.0
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



This article was previously published under Q323635



SYMPTOMS
Under stress conditions that involve heavy memory use, a program that was written in Visual C++ may perform better on a computer that is running Microsoft Windows NT 4.0 than on a computer that is running Microsoft Windows 2000 or Microsoft Windows XP.

When you start the program, it seems to have similar or better performance on a computer running Windows 2000 or Windows XP, and then, after physical memory (RAM) is exhausted, the memory manager starts to use the pagefile. When this occurs, the program on the computer running Windows 2000 or Windows XP may slow down compared to the same program on the computer running Windows NT 4.0. When you view the Task Manager on the computer running Windows 2000 or Windows XP, the CPU usage may peak at 100 percent.



CAUSE
Typically, programs that are written in Visual C++ make allocations through the C-Runtime (CRT). These allocations are handled differently on a computer running Windows NT 4.0 than on a computer running Windows 2000 or Windows XP. When the CRT detects that the program is running on Windows NT 4.0, it uses its own small-block heap for small allocations. The CRT does this to increase heap performance on Windows NT 4.0.

In Windows 2000 and Windows XP, the heap manager has been improved so that the CRT does not need the small-block heap. On a computer running Windows 2000 or Windows XP, the CRT sends all of the allocations to the default process heap. However, under stress conditions, the use of the small-block heap on a computer running Windows NT 4.0 may cause memory operations to be more efficient than the same operations on a computer running Windows 2000 or Windows XP.



RESOLUTION
You can force the use of the small-block heap on all operating systems. To do this, add the following code to the beginning of your program: #include 

int sbh = _set_sbh_threshold(1016); This code sets the small-block heap threshold to 1016 bytes, so that any allocations that have a size equal to or less than 1016 bytes are allocated on the private CRT heap. 1016 is the hard-coded value that MAX_ALLOC_DATA_SIZE is set to in the CRT source. The CRT uses MAX_ALLOC_DATA_SIZE as the default value to initialize the small-block heap on a computer running Windows NT 4.0.

Note There may be other differences between operating systems that cause performance variations. This is only one potential cause.



STATUS
This behavior is by design.



MORE INFORMATION
Typically, the performance difference occurs only when memory usage exceeds the physical memory. The symptoms also seem to be influenced by the types and amounts of allocations that the program makes.

To improve performance, you can also evaluate the application's memory usage. The following are examples of what conditions to look for:  Memory leaks.

Memory may be increasing steadily for a good reason, but you must verify that there are no leaks.For additional information about how to track down memory leaks, click the article number below to view the article in the Microsoft Knowledge Base:

268343 Umdhtools.exe: How to Use Umdh.exe to Find Memory Leaks

 Too much heap use.

Look for places in the code where small allocations and deallocations are occurring at a high rate. Consider how to reduce heap use in those areas. This can reduce the risk of heap fragmentation and the performance costs associated with it. Opportunities to consolidate heap operations.

Look for areas where you can allocate one large block of memory instead of many separate allocations. Consider a class that contains ten integer pointers. You can allocate ten separate integers, or you can allocate an array of ten integers. Similarly, you can allocate a structure of ten integers.

