Share via


Heaps (Windows Embedded CE 6.0)

1/6/2010

A heap is a portion of memory that is reserved for an application to allocate and free memory on a per-byte basis, or on a per-4-byte basis. A heap can optimize memory use, and it can isolate an application so it does not have to allow for the differing page sizes of different microprocessors. Therefore, an application can allocate a block in a heap, and then have the OS determine the number of pages necessary for the allocation.

Heap APIs rely on the low-level virtual memory services provided by the virtual memory APIs. Heap memory is the next level above virtual memory. Memory allocations in a heap are not made on 64-KB boundaries and are meant for allocations smaller than 16 KB. Any allocation larger than 16 KB causes the Heap Management System to use virtual memory directly to create a separate heap for the allocation.

When a process is created, a default heap is also created for the process. An application can use the process heap for its memory allocations, and the heap grows and shrinks accordingly. However, performance can suffer if the amount and type of memory allocations in the default heap cause the heap to become fragmented.

When a heap is fragmented, it takes longer to find a place for a new memory allocation. To prevent fragmentation in your process, create separate heaps for similar objects. If a heap exists that contains same-sized objects, the Heap Manager can more easily find a free block that is large enough to hold a new allocation. However, even if fragmentation is manageable through separate heaps, there is still a cost associated with allocating memory.

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

In a Windows Embedded CE OS design, each application has a default, or local, heap created by the OS when the application starts. An application can also create any number of separate heaps. These heaps have the same properties as the local heap, but they are managed through a separate set of heap functions.

Windows Embedded CE uses a single linked list for all heap blocks and uses a first-fit algorithm. Free heap blocks are merged forward on every allocation or free cycle, and new allocation always starts with the last allocated or freed item. This implementation favors small allocations because it is more likely to find the free block on the first try. Re-using freed blocks usually works better if the allocation, or free cycle, comes in as first-in, last-out (FILO) because free blocks are combined better. Using a large heap conserves virtual memory, but is likely to have a negative performance impact. A small heap is better if you have a size limit.

By default, Windows Embedded CE initially reserves 64 KB of virtual memory, but it commits the pages only as they are allocated. If the application allocates more than 64 KB in the local heap, the system allocates more virtual memory by using VirtualAlloc to fulfill the request for memory. You can specify the maximum or initial size when creating the heap. However, you cannot make the heap larger than the maximum size you specify. Making the heap larger might require a separate, disjointed address space that is reserved for the additional space on the heap. Your application must not depend on the local heap's being contained in a single block of virtual address space.

To avoid fragmenting the local heap, it might be better to create a separate heap if you need a series of memory blocks to use for a specified time. An example of this is a text editor that manages 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.

Note

Windows Embedded CE supports the same code for heaps as many Windows-based desktop platforms. The only important difference that Windows Embedded CE does not support the HEAP_GENERATE_ EXCEPTIONS flag.

To reduce the impact on allocating heap memory, a process should allocate heap memory before proceeding with normal processing, by using HeapAlloc. In general, VirtualAlloc should be used, rather than calling HeapAlloc directly, because VirtualAlloc deprecates direct heap calls.

Windows Embedded CE heaps work best for fixed-size items. To optimize heap usage, make your heap allocation as uniform as possible. If not, you can create separate heaps for different size ranges.

See Also

Concepts

Memory Architecture

Other Resources

Memory Management Reference
HeapAlloc