A custom allocator in C++ is a class that tells a container how to obtain and release memory, and std::pmr (polymorphic memory resources, C++17) is the standard way to swap allocation strategies at run time without changing the container's type. With std::pmr::vector<int> you pass a std::pmr::memory_resource*, such as a monotonic buffer on the stack, and the container allocates from it.
Use these tools when general-purpose heap allocation is too slow, too fragmented, or when you want to free many small objects at once.
The default std::allocator calls operator new, which is flexible but not free. Each call takes a lock or per-thread cache path, adds bookkeeping, and may fragment memory. Situations where a custom strategy helps:
Measure before changing anything. A custom allocator that does not show up as a bottleneck in a profiler adds complexity for no gain.
Before C++17 the allocator was a template parameter of the container, so containers with different allocators were different types. A minimal modern allocator needs value_type, allocate, deallocate, and equality operators. Since C++11 std::allocator_traits fills in the rest.
The type std::vector<int, CountingAllocator<int>> differs from std::vector<int>, so you cannot pass one where the other is expected. That is the main pain of classic allocators, and the problem std::pmr solves.
std::pmr lives in <memory_resource>. A std::pmr::memory_resource is an abstract base class with virtual allocate and deallocate. The container aliases such as std::pmr::vector<T> and std::pmr::string use std::pmr::polymorphic_allocator<T>, which forwards to a resource pointer. Every std::pmr::vector<int> has the same type regardless of the resource behind it.
The vector and its strings all allocate from the stack buffer. Nested pmr containers propagate the allocator to their elements through uses-allocator construction, which is why the strings above also draw from the arena. When the buffer is exhausted, the resource falls back to the upstream resource given as the last argument.
| Resource | Behaviour | Typical use |
|---|---|---|
new_delete_resource() | Calls operator new and delete | Default upstream |
null_memory_resource() | Throws std::bad_alloc on every allocation | Guarantee no heap use |
monotonic_buffer_resource | Hands out memory by bumping a pointer; frees only on destruction or release() | Per-request or per-frame scratch data |
unsynchronized_pool_resource | Size-class pools, not thread-safe | Many small objects in one thread |
synchronized_pool_resource | Same, thread-safe | Pools shared across threads |
A monotonic resource never reuses freed memory while alive, so it suits workloads that build a structure, use it, and discard everything. A long-running loop that keeps allocating and freeing through it will grow without bound.
Derive from std::pmr::memory_resource and implement three private virtual functions.
Pass its address to any pmr container. The resource must outlive every container that uses it, so declare the resource before the containers.
std::pmr::set_default_resource affects all pmr containers that do not name a resource. Use it sparingly in libraries.swap and move assignment behave differently, so read the container's allocator propagation rules.Prefer std::pmr for new code since it keeps container types uniform and ships ready-made pools and arenas. Write a classic allocator only when you need zero virtual-call overhead or must interoperate with existing allocator-templated code.
It is part of C++17 and supported by current GCC, Clang and MSVC standard libraries. Compile with -std=c++17 or later.
They return it to the resource, but a monotonic resource ignores that and keeps the memory until it is released or destroyed.
Only if you make it so. unsynchronized_pool_resource and monotonic_buffer_resource are not thread-safe; use synchronized_pool_resource or one resource per thread.
Custom allocators control where container memory comes from, and std::pmr makes that choice a run-time value while keeping container types the same. Use monotonic_buffer_resource for scratch data, pool resources for many small objects, and a custom memory_resource for logging or special regions. Profile first, and keep resources alive longer than the containers using them.