INF: Handle Limits and Protected Mode Windows

ID Number: Q68644

3.00

WINDOWS

docerr

Summary:

Page 16-25 of the "Microsoft Windows Software Development Kit Guide to

Programming" states:

Under the Windows standard mode and 386 enhanced mode memory

configurations, there is a system-wide limit of 8192 global memory

handles, only some of which are available to any given application.

This statement is incorrect. The limit is 4096 in Windows standard

mode and 8192 in Windows 386 enhanced mode. This is because standard

mode uses two table entries for each segment.

The following information discusses techniques to effectively manage

memory within these limitations.

More Information:

The limit of 8K on the number of handles is a limit imposed by the

architecture of the Intel 80286 and 80386 chips. The protection

mechanism provided by these chips must store all its data in one

memory segment. Windows sets up a local descriptor table (LDT) that is

used by the chip to store vital information about each globally

allocated block of memory. Because each global block of memory

requires 8 bytes of space in the LDT, there is a limit of 8192 blocks

(64K / 8 bytes = 8K). This is a limitation imposed by the hardware.

For more information, see pages 100-102 in "The 80386 Book," by Ross

P. Nelson (Microsoft Press).

There is no way to determine how many selectors a given application

can use. GlobalAlloc will fail when there is not enough memory to

satisfy a given request, or when there are not enough handles. Either

way, Windows is unable to satisfy the application's memory request. It

is extremely important that handles be used with discretion. Each

application must cooperate with other Windows applications. Instead of

allocating many blocks from the global heap, an application should

allocate fewer blocks that are larger in size. Each block can be

divided up functionally by the application.

If small memory blocks are required by the application, use the local

memory-management routines provided by Windows. Local handles do not

impact the selector limit at all.

If an application needs more then 64K of memory, allocate the memory

globally. It may be necessary to use GlobalAlloc to get a large piece

of memory and then store many small objects in it.

In some circumstances, it may be advantageous to employ a more

sophisticated memory-management scheme called multiple FAR heaps. This

technique is useful if the application requires a number of blocks of

memory, say 100, and the total number of bytes in each block is not

above 64K. Chapter 18 (pages 707-724) of "Windows 3.0 Power

Programming Techniques," by Paul Yao and Peter Norton (Bantam Computer

Books), contains more details on this technique. In this chapter,

Norton and Yao state the steps necessary to perform local heap

allocation in a dynamically allocated segment. This chapter

effectively describes the technique of using multiple FAR heaps and

also provides some sample code.

This technique involves four steps:

1. Globally allocate a block of memory with GlobalAlloc.

2. Lock the block of memory with GlobalLock.

3. Initialize the block of memory by calling LocalInit.

4. Modify the local memory-management routines so each will update the

DS register to point to the new heap just before a call to a local

memory-management routine. When access to the new heap is complete,

immediately restore the DS register.

Overall, this technique uses fewer global handles and less memory

overhead with each call.

Additional reference words: 3.00 3.0