A pool used for optimizing memory allocation when large blocks of memory must be repeatedly allocated, deallocated, and then reallocated.


CMMemoryPool is a memory allocation service that holds onto a pool of recently deallocated memory. It’s purpose is to speed up subsequent allocations of the same size. This API is intended for cases where large memory blocks need to be repeatedly allocated. One example would be a video encoding application which outputs compressed data.

All of its allocations are on the granularity of page sizes. It does not sub allocate memory within pages, so it is a poor choice for allocating tiny blocks. For example, it's appropriate to use CMMemoryPool as the blockAllocator argument in the CMBlockBufferCreateWithMemoryBlock method, but not as the structureAllocator argument, use kCFAllocatorDefault instead.

When you no longer need to allocate memory from the pool, call CMMemoryPoolInvalidate and CFRelease. Calling CMMemoryPoolInvalidate tells the pool to stop holding onto memory for reuse. Note that the pool's CFAllocator can outlive the pool, owing to the way that CoreFoundation is designed: CFAllocators are themselves CF objects, and every object allocated with a CFAllocator implicitly retains the CFAllocator until it is finalized. After the CMMemoryPool is invalidated or finalized, its CFAllocator allocates and deallocates with no pooling behavior.

CMMemoryPool deallocates memory if it has not been recycled in 0.5 second, so that short-term peak usage does not cause persistent bloat (This period may be overridden by specifying kCMMemoryPoolOption_AgeOutPeriod). Such "aging out" is done during the pool's CFAllocatorAllocate and CFAllocatorDeallocate methods.


Managing CMMemoryPool

func CMMemoryPoolFlush(CMMemoryPool)

Deallocates all memory the pool was holding for recycling.

func CMMemoryPoolInvalidate(CMMemoryPool)

When CMMemoryPoolInvalidate is called the pool's allocator stops recycling memory.

func CMMemoryPoolGetTypeID() -> CFTypeID

Returns the unique identifier of an opaque type to which the CMMemoryPool object belongs.

Data Types

class CMMemoryPool

A reference to a CMMemoryPool object.


Memory Pool Age Out Period

You specify this key in a Core Foundation dictionary when calling functions such as CMMemoryPoolCreate(options:).

See Also



A simple, lockless FIFO queue of (void *) elements.


A queue of timed buffers.