HeapCreate creates and returns a handle to a private heap. The handle is used to identify the heap in the other Heap functions. The HeapCreate function specifies an initial size which determines the number of committed, read/write pages that are initially allocated for the heap. The maximum size determines the number of additional reserved pages that are allocated to reserve a contiguous block of the virtual address space. Additional pages are automatically committed from this reserved space if allocation requests exceed the initial size (assuming that the physical storage is available). However, once the pages are committed, they are not decommitted until the process terminates or the heap is destroyed by calling HeapDestroy . The memory of the heap is visible only to the process that created it. If a DLL creates a private heap, each process that links to the DLL will cause a separate instance of the private heap to be created that is not visible to the other DLL users.
HeapAlloc allocates blocks of memory from a private heap and returns a pointer to the allocated block. The pointer can be used in HeapFree to release the block, or in HeapSize to determine the size of the block. Memory allocated by the Heap functions is not movable (except for normal paging at the granularity of 4KB pages). So if you are allocating and freeing many small blocks, it is possible for a private heap to become fragmented.
If HeapAlloc fails because the request exceeds either the available space in the heap or the available physical memory, an exception is raised. This allows the try..except structured exception handling to be used (refer to the example above in the section on the Virtual functions). Typically, the exception handler in this case would just clean up the application and exit, since the heap cannot be grown or reallocated. If the except block handles the exception but does not exit, HeapAlloc will return NULL.
A possible use for the Heap functions would be to create a heap when a process starts up, specifying an initial size sufficient to satisfy the memory requirements of the process. If the HeapCreate call failed, the process could terminate or issue some sort of memory warning; but if it succeeded, the process is assured of having the memory when it needs it.