A Memory Pool is a custom memory management strategy that replaces standard malloc or new calls.
Instead of asking the operating system for small chunks of memory one by one, you request one massive block of memory upfront and manage it yourself.
How a Memory Pool Works
The “Arena” or “Buffer”
Allocate a giant char array, or std::vector<char>, on the heap once:
size_t poolSize = 1024 * 1024 * 100; // 100 MB
char* arena = new char[poolSize];
char* currentPtr = arena; The Allocation (The Bump Allocator)
When you need memory for an object, you just move a pointer forward. This is called a Bump Allocator. It is virtually instantaneous (one addition operation).
void* my_alloc(size_t size) {
void* ptr = currentPtr;
currentPtr += size; // Just "bump" the pointer
return ptr;
}Types of Pools
A. Fixed-Size Block Pool
The pool is divided into equal chunks (e.g., 64 bytes each).
- Best for: Storing thousands of identical objects (like
Particlestructs in a simulation). - Pros: Extremely fast; no fragmentation.
B. Arena (Monotonic) Allocator
You keep bumping the pointer until the task is done, then you reset the whole pool at once to “delete” everything.
- Best for: Per-frame data in a game engine or per-request data in a web server.
- Pros: No individual
deletecalls are needed; incredible performance.