Microsoft KB Archive/38941

From BetaArchive Wiki

Free Memory Reported in the MS-DOS Executive About Box

PSS ID Number: Q38941 Article last modified on 11-05-1993

2.10 3.00 MS-DOS

The information in this article applies to:
- Microsoft Windows Software Development Kit for Windows version 3.0


Because my application consumes large chunks of global memory, I would like to inform the user of how much of this precious resource is still available. In a sample program, I found a reference to the command GlobalCompact() with -1 as argument. This call is supposed to return the total number of bytes free (instead of the largest chunk if called with argument 0). However, the figure returned is between 3K and 9K below the figure reported in the About box in the MS-DOS Executive. Is there a better (more exact) way to determine the amount of free global memory?


GlobalCompact(-1) and GlobalCompact(0) are similar. Both report the largest contiguous block of free conventional memory that could be allocated if discarding and compacting take place. The difference between them is that GlobalCompact(-1) actually compacts and discards segments, while GlobalCompact(0) calculates the result without actually causing the effect. In general, neither one will return the same number as reported by the MS-DOS Executive. The Executive finds the largest contiguous block as well as all free blocks sandwiched between fixed code and data segments. To find all free memory available to your application, repeatedly call GlobalCompact(0) followed by a temporary GlobalAlloc() using the number of free bytes returned from the GlobalCompact(0) call. Repeat these steps until GlobalCompact() fails to find any free memory. Sum all the bytes allocated in each step, then free these allocated blocks. This is the total free memory available to your application. Your application may or may not be able to use the small fragmented blocks of free memory efficiently. Under LIM Version 4.0 Expanded memory, the above steps will continue to locate all of the free memory available to your application, but will have no relation to the number reported by the MS-DOS Executive. For background information on EMS, please refer to the Knowledge Base article originally published in the “Microsoft Systems Journal” on the use of LIM Version 4.0 expanded memory by Windows Version 2.x.

KBCategory: KBSubcategory: KrMmGlobalmem Copyright Microsoft Corporation 1993.