Memory pool
lastmod 2024-09-13created 2016-11-1110 edits
A memory pool allocates fixed-size blocks, enabling dynamic memory allocation similar to malloc or the C++ new operator. Facilities such as malloc or the new operator cause fragmentation because of their varying block sizes, and fragmented memory becomes unusable in real-time systems due to performance. A more efficient approach is to preallocate a number of memory blocks of the same size, called a memory pool. Applications can then allocate, access, and free blocks, represented by handles, at run time.
Many real-time operating systems use memory pools, such as the Transaction Processing Facility.
Some systems, such as the Apache web server, use the term memory pool for managing allocations of varying sizes that are later freed all at once. This is also called a region.
Sample memory pool implementation
A simple memory pool module can allocate, at compile time, three pools with block sizes optimized for the application. The application can allocate, access, and free memory through the following interface.
- Allocate memory from the pools. The function determines the pool where the required block fits. If all blocks of that pool are already reserved, the function tries to find one in the next larger pool. An allocated memory block is represented by a handle.
- Get an access pointer to the allocated memory.
- Free a previously allocated memory block.
- For example, the handle can be implemented as an unsigned int data type. Internally, the module can split the handle and interpret it as a pool index, a memory block index, and a version. The pool and memory block indexes allow fast access to the block corresponding to the handle, and the version allows detecting handles of memory blocks that have already been freed.
Memory pool vs malloc
Pros
- Memory pools allow memory allocation with constant execution time (without fragmentation). Whereas each object allocated with malloc must be freed individually, the memory of many objects allocated within a pool can be freed in a single operation.
- Memory pools can be grouped hierarchically, and such grouping can suit special programming structures such as loops and recursive functions.
- Fixed-size block memory pools do not need to store metadata or descriptions, such as the size of the allocated block, for each allocated memory block. This saves considerable space, especially for small allocations.
Cons
- Memory pools must be tuned for the application that uses them.
Implementation
(ACE_Dynamic_Cached_Allocator) See also
- Fragmentation
- Low Fragmented Heap (LFH; in Windows 7)
Favorite site
- Wikipedia (en) Memory pool
- C++ Memory Management Innovation: GC Allocator
- Tutorial - Memory Pool
- Programming with Memory Pools - Chapter 8. Developer Information
- GpgStudy - Simple memory pool implementation (C++)
- Chapter 4, Small object allocation - 1: What is a memory pool and why is it needed? :: Choi Ikpil's nameless blog
- C++ Memory Pools and Angra
- [Recommended] Study collection :: Pooling (memory pool, object pool, STL pooling)