Most applications allocate smaller blocks than the 64-KB minimum allocation granularity possible using page granularity functions such as VirtualAlloc and VirtualAllocExNuma.
- Allocating such a large area for relatively small allocations is not optimal from a memory usage and performance standpoint.
- To address this need, Windows provides a component called the heap manager, which manages allocations inside larger memory areas reserved using the page granularity memory allocation functions.
- The allocation granularity in the heap manager is relatively small: 8 bytes on 32-bit systems, and 16 bytes on 64-bit systems.
- The heap manager exists in two places: Ntdll.dll and Ntoskrnl.exe. The C runtime (CRT) uses the heap manager when using functions such as malloc, free, and the C++ new operator.
- The most common Windows heap functions are:
- HeapCreate or HeapDestroy. Creates or deletes, respectively, a heap. The initial reserved and committed size can be specified at creation.
- HeapAlloc. Allocates a heap block.
- HeapFree. Frees a block previously allocated with HeapAlloc.
- HeapReAlloc. Changes the size of an existing allocation (grows or shrinks an existing block).
- HeapLock or HeapUnlock. Controls mutual exclusion to the heap operations.
- HeapWalk. Enumerates the entries and regions in a heap.