
Memory pools, also called
fixed-size blocks allocation, is the use of
pools for
memory management
Memory management (also dynamic memory management, dynamic storage allocation, or dynamic memory allocation) is a form of Resource management (computing), resource management applied to computer memory. The essential requirement of memory manag ...
that allows
dynamic memory allocation
Memory management (also dynamic memory management, dynamic storage allocation, or dynamic memory allocation) is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dyna ...
. Dynamic memory allocation can, and has been achieved through the use of techniques such as
malloc and
C++'s
operator new; although established and reliable implementations, these suffer from
fragmentation because of variable block sizes, it is not recommendable to use them in a
real time system due to performance. A more efficient solution is preallocating a number of memory blocks with the same size called the memory pool. The application can allocate, access, and free blocks represented by
handles at
run time.
Many
real-time operating system
A real-time operating system (RTOS) is an operating system (OS) for real-time computing applications that processes data and events that have critically defined time constraints. A RTOS is distinct from a time-sharing operating system, such as Unix ...
s use memory pools, such as the
Transaction Processing Facility.
Some systems, like the web server
Nginx
(pronounced "engine x" , stylized as NGINX or nginx) is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Russian developer Igor Sysoev and publicly released in 20 ...
, use the term ''memory pool'' to refer to a group of variable-size allocations which can be later deallocated all at once. This is also known as a ''region''; see
region-based memory management.
Simple memory pool implementation
A simple memory pool module can allocate, for example, three pools at
compile time
In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
with block sizes optimized for the application deploying the module. The application can allocate, access and free memory through the following interface:
*Allocate memory from the pools. The function will determine the pool where the required block fits in. If all blocks of that pool are already reserved, the function tries to find one in the next bigger pool(s). An allocated memory block is represented with a
handle
A handle is a part of, or an attachment to, an object that allows it to be grasped and object manipulation, manipulated by hand. The design of each type of handle involves substantial ergonomics, ergonomic issues, even where these are dealt wi ...
.
*Get an access pointer to the allocated memory.
*Free the formerly allocated memory block.
*The handle can for example be implemented with an
unsigned int
. The module can interpret the handle internally by dividing it into pool index, memory block index and a version. The pool and memory block index allow fast access to the corresponding block with the handle, while the version, which is incremented at each new allocation, allows detection of handles whose memory block is already freed (caused by handles retained too long).
Memory pool vs malloc
Benefits
*Memory pools allow memory allocation with constant execution time. The memory release for thousands of objects in a pool is just one operation, not one by one if ''malloc'' is used to allocate memory for each object.
*Memory pools can be grouped in hierarchical tree structures, which is suitable for special programming structures like
loops and
recursion
Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
s.
*Fixed-size block memory pools do not need to store allocation metadata for each allocation, describing characteristics like the size of the allocated block. Particularly for small allocations, this provides substantial space savings.
*Allows deterministic behavior on real-time systems by avoiding out of memory errors.
Drawbacks
*Memory pools may need to be tuned for the application which deploys them.
See also
*
Free list
*
Object pool
*
Slab allocation
External links
Fast Efficient Fixed-Sized Memory Pool*
ttp://gee.cs.oswego.edu/dl/html/malloc.html A Memory AllocatorProgramming with Memory Pools
{{DEFAULTSORT:Memory Pool
Memory management