HOME

TheInfoList



OR:

Memory pools, also called fixed-size blocks allocation, is the use of pools for
memory management Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when ...
that allows
dynamic memory allocation Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when ...
comparable to malloc or C++'s operator new. As those implementations suffer from
fragmentation Fragmentation or fragmented may refer to: Computers * Fragmentation (computing), a phenomenon of computer storage * File system fragmentation, the tendency of a file system to lay out the contents of files non-continuously * Fragmented distributi ...
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 applications that processes data and events that have critically defined time constraints. An RTOS is distinct from a time-sharing operating system, such as Unix, which ...
s use memory pools, such as the
Transaction Processing Facility Transaction Processing Facility (TPF) is an IBM real-time operating system for mainframe computers descended from the IBM System/360 family, including zSeries and System z9. TPF delivers fast, high-volume, high-throughput transaction processi ...
. Some systems, like the web server
Nginx Nginx (pronounced "engine x" ) 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 Igor Sysoev and publicly released in 2004. Nginx is free and open-source software ...
, 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 In computer science, region-based memory management is a type of memory management in which each allocated object is assigned to a region. A region, also called a zone, arena, area, or memory context, is a collection of allocated objects that c ...
.


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 computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
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 attachment to, an object that allows it to be grasped and manipulated by hand. The design of each type of handle involves substantial ergonomic issues, even where these are dealt with intuitively or by following tr ...
. *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
loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, ...
s and
recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematic ...
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 avoiding the 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






{{DEFAULTSORT:Memory Pool Memory management