Skip to content
Tutorials Center
My learning

Topics

Lesson 150 of 171 ยท Modern C++ and Beyond

Allocators and std::pmr

Learn C++ allocators and std::pmr - write a minimal custom allocator, use monotonic buffer and pool resources, pmr containers, allocator propagation and when it pays off.

Every standard container asks someone else for memory. By default that someone is std::allocator, which calls operator new, which usually ends up in malloc. For most programs that is exactly right. But sometimes the general-purpose heap is the bottleneck: a game that builds thousands of small objects per frame, a request handler that allocates a hundred little strings and throws them all away at once, or an embedded system with a fixed memory budget. In those cases you want to control where the memory comes from.

An allocator is the object a container uses to get and release memory. This lesson shows the classic allocator model with a small custom allocator, then the C++17 polymorphic memory resources in <memory_resource> (std::pmr), which make the same customization far easier. You will measure allocations with a counting resource, put a container on a stack buffer, and learn the one propagation rule that decides whether nested containers cooperate. If you have not met containers yet, start with std::vector in depth and dynamic memory.

What the default allocator does

A container does not call new directly. It calls allocate(n) on its allocator for room for n elements, constructs objects in that raw memory, and calls deallocate(p, n) later. The simplest correct allocator is therefore small. The one below forwards to operator new and counts how often it is used, so you can see how many times a std::vector goes to the heap:

C++ A minimal counting allocator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The allocator needs a value_type, allocate, deallocate, equality, and a converting constructor so a container can re-create it for internal node types. That is the entire minimum since C++11; the rest (construct, destroy, pointer typedefs) is supplied by std::allocator_traits. Growth reallocates repeatedly, which is why reserve matters, and why a custom allocator is sometimes just a way to see the problem.

There is a catch. The allocator is part of the container's type. std::vector<int, CountingAllocator<int>> and std::vector<int> are unrelated types that you cannot pass to the same function or assign to each other. That is what std::pmr fixes.

Polymorphic memory resources

std::pmr::memory_resource is an abstract base class with three virtual operations: allocate, deallocate and compare. A single container type, std::pmr::vector<T>, holds a pointer to some resource and calls it virtually. Swap the resource and the memory strategy changes; the type does not.

flowchart LR
    V["pmr::vector&lt;int&gt;"] --> R["memory_resource*"]
    S["pmr::string"] --> R
    R --> M["monotonic_buffer_resource"]
    R --> P["unsynchronized_pool_resource"]
    R --> N["new_delete_resource (default)"]
    M --> U["upstream resource"]

Containers talk to a memory_resource pointer; the resource decides where bytes come from and may chain to an upstream resource.

The standard library ships these ready-made resources:

Resource Behavior
new_delete_resource() Plain operator new / operator delete (the default)
monotonic_buffer_resource Hands out memory by bumping a pointer; deallocate does nothing; everything is freed at once
unsynchronized_pool_resource Pools of fixed-size blocks; not thread-safe
synchronized_pool_resource Same, thread-safe
null_memory_resource() Always throws std::bad_alloc; useful as a "never touch the heap" upstream

Monotonic buffers: allocate fast, free all at once

A monotonic buffer resource is the simplest fast allocator. It takes a block of memory (often an array on the stack), gives out pieces in order, and never reuses a piece. Individual deallocate calls are ignored. When the resource is destroyed or release() is called, all memory goes back in one step. This is ideal for work with a clear lifetime, such as handling a request or one frame of a game.

To see what is happening, the example wraps another resource with a tiny tracking resource that counts requests reaching the upstream (the real heap):

C++ A stack buffer with a heap fallback

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

The first run sends every vector growth step to the heap. The second keeps all of them inside the stack buffer, because the vector's doubling growth totals less than 16 KiB for 1,000 ints (4 KB final size, plus the discarded smaller blocks that a monotonic resource never reuses). In the third, the buffer runs out and the resource asks its upstream resource for a bigger chunk, so the program keeps working instead of failing. Note that a monotonic resource wastes the old, smaller blocks of a growing vector until it is destroyed; call reserve first if you know the size.

A monotonic_buffer_resource never gives memory back while it lives. Do not use it for long-running containers that keep adding and removing elements, such as a queue that runs for hours; it would grow without bound.

Allocator propagation: pmr containers inside pmr containers

The real power appears with nested containers. A std::pmr::vector<std::pmr::string> has two levels of allocation: the vector's array and every string's character buffer. If each string used the default heap, the arena would catch only half of the allocations. The rule of std::pmr is called uses-allocator construction: when a pmr container constructs an element that is itself allocator-aware, it passes its own resource to that element. So the strings automatically allocate from the same arena as the vector.

C++ One arena for a vector and all its strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Because the upstream here is null_memory_resource(), this program proves that nothing escapes to the heap: if the 4 KiB buffer were too small, the next allocation would throw std::bad_alloc rather than silently use malloc. That makes null_memory_resource a good assertion in tests and in memory-constrained code.

Mixing is the practical downside of std::pmr: std::pmr::string and std::string are different types, so interfaces must pick one. Libraries that use std::pmr throughout, or that keep the choice internal, work best.

Pool resources

A pool resource keeps lists of free blocks grouped by size. Allocating and freeing small objects of repeated sizes, such as the nodes of a std::list or std::map, becomes a pointer swap instead of a trip to malloc, and freed blocks are reused. Use unsynchronized_pool_resource when only one thread touches it:

C++
std::pmr::unsynchronized_pool_resource pool;
std::pmr::map<int, std::pmr::string> table(&pool);

Unlike the monotonic buffer, a pool lets memory be reused while it is alive, so it suits long-lived node-based containers. Both can be layered: a pool can use a monotonic buffer as its upstream.

Setting the default resource

std::pmr::set_default_resource(&r) changes what std::pmr::get_default_resource() returns, and containers constructed without an explicit resource use it. This is process-wide, so it suits experiments (for example, make every unspecified pmr container use the null resource in a test to find stray allocations) but not library code.

When to reach for custom allocation

Do not start here. Measure first with a profiler; the default allocator in a modern C library is quite fast. A custom resource pays off when:

  • You allocate very many small objects and release them together (parsers, frames, requests).
  • You must forbid heap use after startup (real-time or embedded code).
  • You want locality: objects allocated one after another sit next to each other in memory, which helps the CPU cache (see performance).

Remember too that smart pointers and containers already handle ownership; allocators only change where bytes come from. Many of the memory problems people try to solve with allocators are better solved by reserve, by move semantics, or by a different data structure.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem does std::pmr solve compared with classic allocator templates?

    With a template allocator parameter, each strategy creates a different container type. A pmr container always has the same type and holds a pointer to a memory_resource that can be swapped.
  2. 2. What does a monotonic_buffer_resource do when you call deallocate on one of its blocks?

    A monotonic resource only moves forward. This makes allocation extremely cheap but means long-lived, constantly changing containers will waste memory on it.
  3. 3. std::pmr::vector<std::pmr::string> v(&arena); Where do the characters of each element string come from?

    A pmr container passes its resource to allocator-aware elements it constructs, so nested pmr containers share one resource without extra code.
  4. 4. Why is null_memory_resource useful as the upstream of a fixed buffer?

    With the null resource upstream, running out of the fixed buffer is an error instead of a quiet heap allocation, which makes the memory limit enforceable.

Summary

  • Containers get memory from an allocator; the default calls operator new.
  • A classic allocator needs value_type, allocate, deallocate, a converting constructor and equality, and becomes part of the container's type.
  • std::pmr containers hold a memory_resource*, so one type supports many strategies.
  • monotonic_buffer_resource is a fast bump allocator for objects with a shared lifetime; pool resources reuse blocks of the same size.
  • Nested pmr containers propagate their resource to elements automatically.
  • null_memory_resource as upstream turns "buffer full" into an error.
  • Measure first; custom allocation is a tool for specific bottlenecks.

Next: std::bit_cast, mdspan and Newer Utilities, a tour of recent additions to the standard library.