Shrinking Heap Space

Last reviewed: July 22, 1997
Article ID: Q21581
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

When an application calls LocalAlloc() and there is not enough memory within the application's data segment, Windows will use memory from the global heap to append this to the application's data segment. Releasing the memory that was temporarily requested then becomes an issue to the programmer.

For example, an initial HEAPWALK shows 12000 bytes free. After a 4K LocalAlloc() and LocalLock(), HEAPWALK shows 4000 bytes locked and 8000 free. Then the program allocates and locks another 10K piece; HEAPWALK shows 4000 bytes locked, 8000 free, and 10000 locked. If the program then deallocates the 4K and 10K blocks with unlock and free, HEAPWALK shows a free 12000 bytes and a free 10000 bytes. The programmer then has the problem of releasing the second free 10K block.

Using the LocalShrink() function will compact and shrink the data segment to the smallest size possible. LocalShrink() cannot move FIXED or locked blocks when compacting the local heap. Therefore, there may still be free space in the heap, and the size of the heap may not be as small as requested after calling LocalShrink(). However, this function will compact as much as possible, given this constraint.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: KrMm
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.