xo-arena Memory Lifecycle

xo::mm::DArena

The life of an arena

An arena reserves a large range of addresses up front and hands it out with a bump pointer. What it uses creeps forward continuously; what it commits — the memory actually backed by pages — can only move in whole superpages, so it advances in jumps.

  1. PHASE 1 mmap(PROT_NONE) Address range reserved. Nothing is backed yet.
  2. PHASE 2 alloc() Bump pointer creeps; commit jumps to keep ahead of it.
  3. PHASE 3 clear() Bump pointer resets. Committed memory is kept.
  4. PHASE 4 unmap() The whole range goes back to the kernel.

Address range

32 MB reserved · 2 MB superpages

current superpage —
— each block is one alloc —
whole reservation 32.0 MB
lo_ hi_
used — payload bytes overhead — header + padding, stripe height is its share available — committed, not handed out uncommitted — addresses only limit_ — commit frontier
used0 B payload
overhead0 B header + padding
allocated0 B free_ − lo_
committed0 B 0 superpages
available0 B committed − allocated
reserved32.0 MB hi_ − lo_
16.0 KB avg

Event log


    

What the four numbers mean

DArena tracks three pointers into one reserved range — lo_, the bump pointer free_, and the commit frontier limit_ — plus the end of the reservation, hi_. Every quantity above is a difference between two of them.

reserved()
hi_ - lo_. Fixed when the arena is built: one mmap with no access rights. Costs address space, not memory.
DArena.hpp:110
committed()
committed_z_. Memory actually backed by pages, moved only by expand() calling mprotect(PROT_READ|PROT_WRITE).
DArena.hpp:117 · DArena.cpp:594
allocated()
free_ - lo_. Everything the bump pointer has passed over, including each allocation's 8-byte header and its padding up to 8-byte alignment.
DArena.hpp:114
used
Payload only — what callers asked for. Together with the per-allocation overhead it makes up allocated(); the gap from there to committed() is available(), memory ready for the next allocation with no syscall.
MemorySizeInfo.hpp:47 · DArena.hpp:120

Why commit moves in jumps

Each allocation asks expand() for allocated() + z1 bytes. If that still fits below limit_, nothing happens — the common case, and the reason allocation is a pointer bump. When it doesn't fit, expand() rounds the target up to the arena's alignment and mprotects the difference in one call:

aligned_target_z = padding::with_padding(target_z, arena_align_z_)

That alignment is the 2 MB hugepage_z_ whenever the requested arena is at least one superpage, and the 4 KB page size otherwise. Hence the staircase: sixteen 2 MB treads across a 32 MB reservation. Turn the superpages off above and the same run climbs in 4 KB steps — so fine that the commit frontier looks continuous, which is exactly the trade. Superpages relieve TLB pressure, at the cost of committing up to 2 MB before it is needed.

Clearing is not freeing

clear() sets free_ = lo_ and re-establishes the guard bytes. It does not touch committed_z_ or limit_. Used and allocated collapse; the physical memory stays with the process, ready to be handed out again without another mprotect. Only unmap() — also what the destructor runs — returns the range to the kernel.

This is a model, not a recording. The arithmetic follows the source — 8-byte alignment, an 8-byte header per allocation, commit targets rounded to arena_align_z_ — but the allocation sizes are an illustrative mix rather than a captured trace: three populations — small objects, buffers, the occasional big one — scaled so the mean matches the object-size slider. Guard bytes are off, matching the default guard_z_ = 0. Overhead is worth driving with that slider: at 16 KB objects the 8-byte header and its padding come to well under a tenth of a percent of what the arena hands out, and at 64 B they cost more than a tenth of it. On the reservation ribbon that share is a stripe through the middle of the allocated block rather than a slab at its end, since the headers are scattered through the range and not gathered anywhere; it is held to a minimum of one pixel so a small share stays visible. Below roughly a pixel per allocation the page view stops drawing boundaries and says so, since at that point they are a grey wash rather than information. Something that ran out of reserve would capture reserve_exhausted and return null rather than growing; this run never gets that far.