5.4 Arenas, Pools, and Immortal Data

MEMORYSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18
Before this page Allocation and Object Layout

Some objects live as long as the process. NULL, TRUE, FALSE and the NA string are created at startup and never freed. GNU R takes them from its ordinary heap (R_NilValue is the first cons cell InitMemory allocates) and keeps them alive as collector roots, so every collection visits them again. Objects like these suit an arena: one contiguous block, a pointer that only moves forward, and no way to free a single object.

5.4.1 Arenas and pools

An arena is a contiguous block of memory. The allocator bumps a pointer through it, so allocation is a bounds check and a pointer increment. Deallocation does nothing.

Arena start: 0x1000
Pointer: 0x1000

Allocate 100 bytes: return 0x1000, pointer becomes 0x1064
Allocate 50 bytes: return 0x1064, pointer becomes 0x1096
Free: ignored

Arenas are efficient for bulk allocation and bulk release. The tradeoff is no fine-grained deallocation. Objects in an arena are freed only when the entire arena is freed.

A pool is a variant. A pool pre-allocates slots of a fixed size and keeps the free ones on a list. Allocation takes the first slot off the list and deallocation pushes the slot back on, so both take constant time.

Pools suit many objects of one size. GNU R’s non-vector nodes are an example: memory.c carves pages into fixed-size SEXPREC slots and keeps the free ones on a list, so allocating a cons cell or an environment takes the next free slot.

arena object 1 object 2 not yet used bump pointer allocate: return the pointer, then move it forward free one object: nothing happens pool in use free in use free free list every slot is the same size allocate takes a free slot; free returns it to the list

An arena only moves its pointer forward and never frees single objects. A pool reuses fixed-size slots through a free list.

5.4.2 The immortal arena

Some data lives for the entire program: builtin constants, symbols, and marker objects such as R_UnboundValue. In GNU R these are ordinary heap objects that C global variables point to, and the collector treats those globals as roots.

In multi-interpreter R, some of them must be shared, because compiled packages compare against them by address (x == R_NilValue). Sharing runs into the garbage collector. R’s mark phase writes into every object it reaches: it sets the mark bit in the object’s header and relinks the object onto a list through pointers inside it. Two interpreters collecting at the same time would both write into the same shared object.

An immortal arena avoids those writes. Objects in it are never collected, and no collector writes to them.

threaded-r allocates an immortal arena at startup. It holds the nine objects GNU R’s collector marks as its builtin constants: R_NilValue (what NULL names), NA_STRING, the empty string R_BlankString and its length-one vector R_BlankScalarString, and the marker symbols R_UnboundValue, R_MissingArg, R_RestartToken, R_CurrentExpression and R_InBCInterpreter. The print names of the last two markers join them, for eleven constants. Later patches added R_EmptyEnv and the 38 symbols that the C API exports by pointer, each with its print name. Every interpreter shares pointers to these objects. Their mark bit stays permanently set, so a collector that reaches one stops before writing anything.

The arena is filled once, at startup, so its allocation cost is paid once and nothing in it fragments. Reading costs nothing extra: every interpreter holds the same pointers, and a test such as x == R_NilValue stays a single pointer comparison.

CPython’s PEP 683 immortal objects solve the same problem for reference counts. An immortal object carries a fixed reference count that Py_INCREF and Py_DECREF leave alone, so the object is never freed and reading it never writes to it, which is what lets interpreters share it.

5.4.3 Object lifetimes and arenas

Most R objects are temporary. A vector created inside a function becomes garbage when the function returns, and the next collection that reaches it frees it: the collector marks everything reachable and frees the rest.

Some objects outlive most others: package namespaces, the global environment, loaded code. These can live in a separate longer-lived arena.

Some objects are permanent. Builtins, constants, symbols. These go in the immortal arena.

An allocator with multiple arenas can tune each one. The short-lived arena is collected often, the long-lived arena rarely, and the immortal arena never. GNU R’s generational collector gets part of this effect without separate arenas: it collects the young generation often and its two older generations less often.

Per-interpreter heaps can use arenas for: immortal builtins (shared), per-interpreter permanent data (package code, namespaces), and temporary data (vectors, environments). Each arena has different collection strategy.

5.4.4 Free lists and recycling

A pool maintains a free list. When an object is freed, it is pushed onto the list. When the allocator needs an object of that size, it takes the first block on the list.

The free list is simple. Each free block holds a pointer to the next free block. The pointer lives in the freed block’s first bytes.

#include <stddef.h>

// Node in the free list
typedef struct FreeNode {
  struct FreeNode *next;
} FreeNode;

static FreeNode *freelist = NULL;

// Allocating from the free list
void *allocate(void) {
  if (freelist == NULL) return NULL;  // pool is exhausted
  FreeNode *node = freelist;
  freelist = node->next;
  return node;
}

// Freeing back to the free list
void free_to_pool(void *ptr) {
  FreeNode *node = (FreeNode *)ptr;
  node->next = freelist;
  freelist = node;
}

Free lists reduce allocation overhead for objects created and destroyed frequently. The cost is memory: a pool keeps its free slots, so it stays near its peak size unless it hands whole empty pages back.

5.4.5 Small vector pool

R allocates many small vectors, and a malloc call for each would pay malloc’s per-block header and bookkeeping every time.

GNU R allocates vectors with up to 128 bytes of data from its own pages, one set of pages per size class (8, 16, 32, 64 and 128 bytes of data), and gives each larger vector a malloc block of its own. A small vector’s slot goes back on its class’s free list after the collection that finds it dead. A page goes back to malloc only when no node on it is in use.

The pool trades memory (reserved but unused slots) for fragmentation reduction and allocation speed.

5.4.6 Per-interpreter arenas

threaded-r already has two pieces of this: the shared immortal arena and a heap per interpreter. A finer split could give each interpreter three regions:

  1. An immortal arena. Shared builtin constants. Interpreter-1 and interpreter-2 both reference the same NULL, the same marker symbols, and so on.

  2. A permanent arena. Per-interpreter namespaces, loaded code, global environment. Allocated once at interpreter startup, kept for the interpreter’s lifetime.

  3. A temporary arena. Short-lived vectors, environments, promises. Collected frequently.

The permanent arena survives across garbage collections. The temporary arena could be reclaimed wholesale only if the collector first copied its survivors out, and R’s collector does not move objects, because C code holds raw pointers to them.

r-in-rust takes a different route. Each interpreter’s heap is an arena of slots addressed by integer handles, collected by a precise, non-moving mark-sweep collector, and every heap reserves the same low handles for its immortal objects, with NIL at handle 0.

5.4.7 Arenas and interpreter isolation

On a shared heap, allocation ordering matters. Interpreter-1 allocates, interpreter-2 allocates, interpreter-1 frees. Fragmentation can make the shared heap inefficient.

With per-interpreter arenas, each interpreter’s temporary arena is isolated. Interpreter-1 and interpreter-2 do not interfere. Each fills its temporary arena independently. Collection is local.

The cost is memory: two interpreters keep two temporary arenas, each with its own unused space.

Further reading

Where this appears

  • threaded-r The immortal arena holds R’s builtin constants once for the whole process, so every interpreter can share them.
  • r-in-rust (rrs) Each interpreter’s heap is an arena of slots addressed by handles, with its immortal objects at fixed low handles.

Where to go next

Back to top