Memory Heap
   HOME

TheInfoList



OR:

Memory pools, also called fixed-size blocks allocation, is the use of pools for memory management that allows dynamic memory allocation comparable to
malloc C dynamic memory allocation refers to performing manual memory management for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely , , , and . The C++ programming language includes ...
or C++'s
operator new In the C++ programming language, and are a pair of language constructs that perform dynamic memory allocation, object construction and object destruction. Overview Except for a form called the "placement new", the operator denotes a request f ...
. 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 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 tra ...
at run time. Many real-time operating systems use memory pools, such as the Transaction Processing Facility. 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.


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. *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, an ...
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 mathematics ...
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 A free list (or freelist) is a data structure used in a scheme for dynamic memory allocation. It operates by connecting unallocated regions of memory together in a linked list, using the first word of each unallocated region as a pointer to the ne ...
*
Object pool The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the po ...
* Slab allocation


External links


Fast Efficient Fixed-Sized Memory Pool






{{DEFAULTSORT:Memory Pool Memory management