---
title: "Memory"
description: "The layered memory model used by Atoll programs and Unit instances."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.atoll.workreef.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Memory

> **Written against the retired host**
>
> This page was written against `atoll-runtime-wasm`, and that host is **gone** —
> it is on the workspace `exclude` list and no longer builds. What runs is
> `crates/atoll-runtime`: one wasmtime `Store` per reactor, with host bodies as
> ordinary `async fn`s reached through the generated imports in `atoll-hosts`.
>
> The **surface** below is current: it is what `stdlib/` declares and what a
> program can name. Statements about *how the host answers* have not all been
> re-measured against the shipping reactor, so read those as the contract a host
> owes rather than as a description of what it does today.

Atoll uses several cooperating memory layers. Calling all of them “the heap”
hides the rules that make values relocatable, asynchronous I/O safe, and Unit
instances independently disposable.

```text
WebAssembly linear memory
└─ link-group allocator
   └─ Unit instance backing allocation
      ├─ FlatBuilderHeader
      └─ RuntimeArena body
         ├─ RuntimeArena header
         ├─ values and collection blocks
         ├─ task control blocks
         ├─ continuation frames
         └─ scheduler nodes
```

The link-group allocator obtains raw ranges from WebAssembly linear memory. A
Unit instance owns one of those ranges as its arena. Atoll values inside the
arena use flat, offset-based layouts.

**Guest values come from `segalloc`.** Every
`alloc`, `realloc` and `dealloc` a compiled program makes reaches
`atoll_heap::segalloc::Heap`, opened at the address the instance context names,
and the runtime's cycle collector opens the same heap. It is a
**class-segregated** allocator: one size class per 16 KiB segment, four classes
per octave, per-segment free lists, an exactly-sized allocation for anything
past a quarter of a segment, and a segment whose last block dies goes straight
back. Two properties of that shape are load-bearing above it — a block's start
is recoverable from **any interior address** in O(1) with no memory access,
which is what lets a reference into a buffer keep the buffer alive; and an
arena is a *set* of segments rather than one range, so growth adds a segment
and moves nothing.

This chapter describes the current WebAssembly runtime implementation. Layouts
and generated-code interactions are runtime ABI contracts. Allocation
thresholds and growth heuristics are implementation policy unless a page says
otherwise.

## Core rules

The memory model rests on six rules:

1. A Unit instance owns a persistent arena. It is not recreated for every
   function call or every invocation.
2. An arena-relative offset is the durable address of in-arena data. A native
   pointer into WebAssembly memory is temporary.
3. Offset `0` is null or the empty niche. Valid allocations therefore have
   nonzero offsets.
4. Relocating an arena changes its absolute address but preserves its internal
   offsets.
5. Compiler-inserted ownership operations reclaim ordinary acyclic data.
   Cycles are reclaimed by **trial deletion** — a shadow-count scan of a
   candidate's own subgraph, not a trace of the live heap.
6. Destroying or poisoning a Unit instance releases its complete arena as one
   backing allocation.

## Memory layers

| Layer | Owner | Lifetime | Address form |
|---|---|---|---|
| Linear memory | link-group WebAssembly instance | Store lifetime | absolute Wasm address |
| Raw allocator | compiled link group | link-group instance | absolute Wasm range |
| Runtime arena | Unit instance | instance cache lifetime | body base plus relative offset |
| Invocation frames | one invocation | invocation lifetime | arena-relative offset |
| Host resources | Unit instance host state | resource lifetime | generation-tagged handle |

Host resources such as sockets and database handles are not stored inside the
arena. The arena contains compact handles; the runtime keeps the corresponding
host objects in a generation-checked slab.

## Addressing

Generated code normally manipulates a `u32` offset relative to the arena body:

```text
absolute Wasm address = current arena body base + arena-relative offset
```

The runtime republishes the body base after growth. Code that materializes a
raw address must not retain it across an operation that can grow, suspend, or
otherwise relocate the arena. A generation counter lets generated and host
code detect that a previously materialized address belongs to an older arena
generation.

The `u32` offset representation imposes a design ceiling of 4 GiB for one
arena. Runtime configuration and allocation checks may impose smaller practical
limits.

## Lifetimes

The arena contains data with different logical lifetimes:

- invocation frames and scheduler bookkeeping are temporary;
- Unit state and reachable values can survive across invocations;
- asynchronous operations can keep arena ranges live across suspension;
- retired arena generations survive only while an operation has pinned them.

The distinction matters during cleanup. Normal invocation completion releases
the temporary frame region, but the Unit instance and its arena remain cached.
Termination can leave frames and reference counts indeterminate, so a poisoned
instance is evicted wholesale instead of attempting ordinary destructor
execution.

## Read next

- [Arenas](https://docs.atoll.workreef.dev/runtime/memory/arenas/) defines the arena's growth and generation
  protocol.
- [Flat Values](https://docs.atoll.workreef.dev/runtime/memory/flat/) defines the layouts stored in an arena.
- [Ownership](https://docs.atoll.workreef.dev/runtime/memory/ownership/) connects compiler ownership modes,
  reference counting, reuse, and cycle collection.
- [Memory](https://docs.atoll.workreef.dev/language/memory/) is the same model from a program's side: where a
  release lands, where a `^T` may travel, and what a cycle costs.
- [Units](https://docs.atoll.workreef.dev/runtime/units/) explains why each Unit instance owns an arena.
- [Host I/O](https://docs.atoll.workreef.dev/runtime/host-io/) explains how suspended operations borrow or pin
  arena memory.

## Implementation

The primary implementation is in `atoll-heap`, `atoll-heap-wasm`,
`atoll-abi`, and `atoll-runtime`. Generated layouts remain
authoritative when an older design document disagrees with compile-time size or
offset assertions.

Source: https://docs.atoll.workreef.dev/runtime/memory/index.mdx
