The HeapCompact function attempts to compact a specified heap. It compacts the heap by coalescing adjacent free blocks of memory and decommitting large free blocks of memory.
UINT HeapCompact(
HANDLE hHeap, // handle to the heap to compact
DWORD dwFlags // bit-flags that control heap access during
// function operation
);
Value | Meaning |
---|---|
HEAP_NO_SERIALIZE | Specifies that mutual exclusion will not be used while the HeapCompact function accesses the heap. |
If the function succeeds, the return value is the size, in bytes, of the largest committed free block in the heap. This is an unsigned integer value.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
In the unlikely case that there is absolutely no space available in the heap, the function return value is zero, and GetLastError returns the value NO_ERROR.
There is no guarantee that an application can successfully allocate a memory block of the size returned by HeapCompact. Other threads or the commit threshold might prevent such an allocation.
Serialization ensures mutual exclusion when two or more threads attempt to simultaneously allocate or free blocks from the same heap. There is a small performance cost to serialization, but it must be used whenever multiple threads allocate and free memory from the same heap. Setting the HEAP_NO_SERIALIZE flag eliminates mutual exclusion on the heap. Without serialization, two or more threads that use the same heap handle might attempt to allocate or free memory simultaneously, likely causing corruption in the heap. The HEAP_NO_SERIALIZE flag can, therefore, be safely used only in the following situations:
Windows NT: Requires version 3.5 or later.
Windows: Unsupported.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Memory Management Overview, Memory Management Functions, HeapCreate, HeapValidate