A memory pool , also called fixed-size block allocation , is the use of pool (s) for memory management , which provides dynamic memory allocation comparable to the malloc function or the new operator in C ++ . Since these implementations suffer from fragmentation (due to variable block sizes), it is not recommended to use them in real-time systems due to performance problems. A more effective solution is to pre-allocate several blocks of memory of the same size, which are called the memory pool . An application can allocate, use, and free blocks provided by an assistant right at runtime .
Many real-time operating systems use memory pools, for example, in a Transaction Processing Object .
Some systems, such as the Nginx web server, use the term memory pool to refer to a group of operations for allocating memory of different sizes, which can be later freed all at once. It is also known as a region ; see memory management at the regional level .
Content
Simple implementation of a memory pool
A simple memory pool module can allocate, for example, three pools at compile time with block sizes optimized for use in this application. An application can allocate, access and free memory through the following interface:
- Allocate memory from the pool. The function will determine the pool in which the required memory block will fit. If all blocks of the current pool are already reserved, then the function tries to find the next in the next larger pool. The allocated memory block is presented with an assistant .
- Get a pointer to access the allocated memory.
- Free the previously allocated memory block.
- Helper can be implemented, for example as
unsigned int. The module can interpret the assistant inside itself, dividing it by the pool index, memory block index and version. The pool and index of the memory block allows you to quickly access the corresponding block through the assistant, while the version, which increases with each new allocation, allows you to find assistants whose memory block is already freed (due to too long storage).
Memory pool vs malloc
Benefits
- Memory pools allow you to allocate memory for a fixed time. The release of thousands of objects from memory is performed in one operation, and not many, as is the case with a separate memory allocation for each object through malloc .
- Memory pools can be grouped into a hierarchical tree, which is convenient when implementing loops and recursions .
- A fixed-size memory pool does not require metadata for each memory allocation, which should describe characteristics such as the size of the allocated block. From a practical point of view, for small memory allocations, this gives significant space savings.
- Allows you to implement deterministic behavior in real-time systems and avoid memory errors.
disadvantages
- Memory pools may need to be configured for the application that uses them.
See also
- Exemption list
- Object pool
- Slab Distribution