Microsoft Windows CE 3.0  

Heaps

Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

To optimize memory use, an application must be able to allocate and free memory on a per byte, or at least a per 4 byte, basis. The system provides this ability through heaps. Using heaps also isolates an application from having to deal with the differing page sizes of the microprocessors that support Windows CE. An application can simply allocate a block in a heap and the system will deal with the number of pages necessary for the allocation.

Unlike Windows NT or Windows 98, Windows CE only supports allocating fixed blocks in a heap. This simplifies the handling of blocks in the heap but it can lead to the heaps becoming fragmented over time as blocks are allocated and freed. The result can be a heap that is almost empty but still requires a large number of virtual pages since the system cannot reclaim a page from the heap unless it is completely free.

In a Windows CE-based platform each application has a default, or local, heap created by the system when the application is launched. An application can also create any number of separate heaps. These heaps have the same properties as the local heap but are managed through a separate set of heap functions.

By default, Windows CE initially reserves 384 pages (on a 1KB page size machine) for the local heap, but only commits the pages as they are allocated. If the application allocates more than the 384KB in the local heap, the system will allocate more space for the local heap. Growing the heap may require a separate, disjointed address space reserved for the additional space on the heap. Your application code must not depend on the local heap being contained in a single block of virtual address space.

To avoid fragmenting the local heap, it is sometimes better to create a separate heap if you need a series of blocks of memory that will be used for a set amount of time. An example of this is a text editor that may manage a file by creating a separate heap for each file it is editing. As files are opened and closed, the heaps are created and destroyed.

Windows CE supports the same API set for heaps as Windows NT or Windows 98. The only noticeable difference is a lack of support for the HEAP_GENERATE_ EXCEPTIONS flag. Under Windows NT, this flag causes the system to generate an exception if an allocation request cannot be accommodated.



 Last updated on Tuesday, May 18, 2004

© 2004 Microsoft Corporation. All rights reserved.