<!-- https://tinyactors.dev/guides/capacity/ -->
# Capacity and memory

Kind: how-to. For: [Agent-platform engineer](https://tinyactors.dev/audiences/agent-platform.xml), [Tech lead who approves dependencies](https://tinyactors.dev/audiences/tech-lead.xml).
Voice: [How-to](https://tinyactors.dev/voices/how-to.xml).

> A tinyactors actor takes about 530 bytes started and 120 hibernated, and each operation costs about the same at four million actors as at two.

On Node.js a started actor takes about 530 bytes and a hibernated one about 120, so four million fit in one process. Each operation costs about the same at four million actors as at two.

<a id="regions"></a>

## Let the host grow its memory

A host keeps the actors of each compiled image in one block of memory, its region. When one of its pools fills, actors or timers for instance, the host copies the region into a larger one.

A host given no sizes starts with room for 64 actors, and grows as the population does.

<a id="the-sizes"></a>

## Set where each pool starts

The `system` map of the `config` option gives each new region its first sizes, in whole numbers. An absent key, or 0, keeps the default.

| Key | Default | What it counts |
|---|---|---|
| `maxActors` | 64 | actors, hibernated ones included |
| `maxEvents` | 256 | events waiting in the actors' queues |
| `maxTimers` | 64 | pending delayed sends |
| `maxRequests` | 16 | running invocations (child charts and services) |
| `workspaces` | 4 | actors in the middle of a step at once (a sender keeps its workspace until its send is handled); never refused: an actor waits for one, and the host adds more |
| `instanceBytes` | derived from the image and `maxActors` | the actors' variables and states |
| `stringBytes` | 16384 + 16 × `maxActors` | strings the actors hold |
| `ticksPerSecond` | 1,000,000,000 | the host's clock rate |

A size is where a pool starts, and the host still grows it. A pool that fills grows between runs, and the other pools stay as they were.

- A pool doubles, or grows up to 16 times at once when doubling would add under a quarter of the region. So a small pool beside a large region grows in a few copies.
- A grown region never shrinks.
- `maxActors` is at most 8,388,606. A run or an input is refused with `TA_ERROR_NO_MEMORY` only at a pool's largest size, or when the allocator refuses memory.

<a id="reading-what-a-host-uses"></a>

## See how full each pool is

`await host.hostStats()` answers with `pools`, one entry per region still in use ([Inspecting a host](https://tinyactors.dev/guides/inspection/index.md)). An entry's `segment` is its image's place among `new Host`'s charts, or what `addImage` returned.

Each pool reports its `capacity`, what it holds in `used`, and the most it held at once in `highWater`.

The pools are `actors`, `events`, `timers`, `workspaces`, `requests` and `slabBytes`, the memory for the actors' variables and strings.

A thousand idle actors grow a host that starts at the default 64, and fit in one sized for 1000. Each region grows on its own, so a second image is a second entry.

```javascript title="stats.mjs"
import { compileCharts, Host } from '@tinyactors/node';

const compiled = compileCharts([{ name: 'idle.scxml', text: `
<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
       datamodel="ecmascript" name="idle"><state id="waiting"/></scxml>` }]);
const image = compiled.image;
compiled.close();

for (const system of [{}, { maxActors: 1000 }]) {
  const host = new Host(image, { system: 'demo', config: { system } });
  const before = (await host.hostStats()).pools[0].actors;
  for (let i = 0; i < 1000; i++) host.spawn('idle');
  const after = (await host.hostStats()).pools[0].actors;
  console.log(`maxActors ${system.maxActors ?? 'default'}: capacity ${before.capacity}, ` +
    `then ${after.used} used, high water ${after.highWater}, grown: ${after.capacity > before.capacity}`);

  host.addImage(image);
  console.log('segments reported:', (await host.hostStats()).pools.map((pool) => pool.segment).join(', '));
  host.close();
}
```

```text title="output"
maxActors default: capacity 64, then 1000 used, high water 1000, grown: true
segments reported: 0, 1
maxActors 1000: capacity 1000, then 1000 used, high water 1000, grown: false
segments reported: 0, 1
```

A migration's old and new versions are two entries too, one per image.

<a id="sizing-a-host-up-front"></a>

## Size a host for a known population

Run a realistic load, read the high-water marks, and give the next host those sizes. A host sized for its load makes no copies, and no pool grows past what the load needs.

Count every actor in `maxActors`, since a hibernated one keeps its slot and its record, about 64 bytes. Size `instanceBytes` and `stringBytes` for the resident actors only.

The repository's hibernation gate sizes a host this way for two million counters of a few variables, 1000 resident at most. `host.hostConfig()` answers the sizes, every default filled in.

```javascript title="size.mjs"
import { Host } from '@tinyactors/node';

const N = 2_000_000; // actors
const R = 1000;      // resident at most
const room = 64;
const host = new Host([], { system: 'big', config: { system: {
  maxActors: N + room, maxEvents: 4096,
  instanceBytes: 2048 * (R + room), stringBytes: 16384 + 64 * (R + room),
} } });
console.log((await host.hostConfig()).system.maxActors);
host.close();
```

```text title="output"
2000064
```

A chart with more variables or longer strings needs more per resident actor. `slabBytes.highWater` under load shows how much.

<a id="pools-that-fill-together"></a>

## Leave room for pools that fill together

A pending delayed send holds a timer and an event, and one falling due briefly holds a second event. So `N` actors that each wait on a delayed send need `maxTimers` of `N` and `maxEvents` above it.

Actors that become runnable together each hold a workspace, against a default of 4. Timers due in the same tick do, and so do actors sending to each other.

On `@tinyactors/wasm` a pool that runs out costs most: the region is copied inside WebAssembly memory, which keeps its peak.

| Pools | Memory per actor |
|---|---|
| `maxTimers` `N`, `maxEvents` `N` and a sixteenth, `workspaces` 1024 | 605 B |
| the default `workspaces`, room for 4,000 events beyond `N` | 1,142 B, grown within ten seconds |

A million actors on `@tinyactors/wasm` 0.4.0, each waiting on a delayed send, in the website's [measurement lab](https://github.com/tinyactors-dev/tinyactors/tree/main/website/examples/lab). Measured 2026-11-21 on an Intel Xeon at 2.10 GHz, 4 CPUs, 16 GiB, Linux; Node.js 22.23.5.

That host reached 35 workspaces at once. A million actors sending to each other reached 92.

<a id="what-an-actor-costs"></a>

## Measure what one actor costs

The [memory program](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/examples/memory) measures what one more actor, message or delayed send adds to a process, part by part. Run it with your chart, on your runtime and datamodel, the engine chart code runs in: native or quickjs.

```shell
node examples/memory/main.mjs --counts 1000000 --datamodel native
```

| runtime, datamodel | actor, started | actor, after an event | actor, hibernated | message waiting | delayed send pending |
|---|---|---|---|---|---|
| node, native | 532 B | 673 B | 118 B | 309 B | 353 B |
| bun, native | 601 B | 781 B | 93 B | 386 B | 448 B |
| node, quickjs | 1036 B | 1634 B | 92 B | 309 B | 358 B |
| bun, quickjs | 1041 B | 1645 B | 90 B | 313 B | 356 B |

What the process holds more for each thing, at a million. Measured 2026-11-25 on an Intel Xeon at 2.10 GHz, 4 CPUs, 16 GiB, Linux; Node.js 22.23.5 and Bun 1.4.1. A counter chart with one `<data>`, messages of `{"text": "hello"}`, tracing on; the hibernated column keeps at most 1000 actors resident and the rest in a store.

On Node.js a started actor takes 532 B of the process at a million, and a hibernated one 118 B.

The core's region and quickjs's heap account for part of it, the same on Node.js and Bun. On the native datamodel most of the rest is the JavaScript engine's heap.

| datamodel | actor, started | actor, after an event | actor, hibernated | message waiting | delayed send pending |
|---|---|---|---|---|---|
| native | 196 B | 196 B | 82 B | 106 B | 151 B |
| quickjs | 796 B | 1372 B | 82 B | 284 B | 329 B |

What the core's reservation, the host's other allocations and quickjs's heap account for, at a million, the same run.

The host's and the engine's fixed costs spread over the population, so an actor costs less in a larger one.

| | 10,000 | 100,000 | 1,000,000 | 2,000,000 | 4,000,000 |
|---|---|---|---|---|---|
| per actor, started | 803 B | 720 B | 532 B | 510 B | 495 B |
| per actor, after an event | 1446 B | 870 B | 673 B | 654 B | 639 B |
| per actor, hibernated | 637 B | 269 B | 118 B | 100 B | 91 B |
| per message waiting | 602 B | 392 B | 309 B | 293 B | 285 B |
| per delayed send pending | 737 B | 489 B | 353 B | 338 B | 331 B |

The process's bytes per thing at each population, Node.js on the native datamodel, the same run.

[The run's CSV](https://github.com/tinyactors-dev/tinyactors/blob/main/tinyactors-napi/examples/memory/results/2026-11-25.csv) holds every part of every row, at every population and on every runtime.

- **A started actor**: holds its variables in the datamodel and its records in the region: 196 B reserved for this chart at a million (81 B, its instance and its strings), 179 B of it used.
- **After an event**: an actor keeps that event as `_event` until the next one. On quickjs the event's record, 106 B, stays in the region too.
- **A hibernated actor**: uses 81 B of records in the region, and its blob is in your store: 145 B on the native datamodel and 147 B on quickjs, for this chart.
- **A message**: costs 106 B in the region (57 B with tracing off) and its data in the datamodel.
- **A delayed send**: holds its message until it falls due: 151 B in the region (102 B with tracing off).
- **On quickjs**: the engine keeps every actor's variables in its own heap, bounded by `quickjs.memoryLimit`, 1 GiB unless the host sets another. A million resident actors after an event need about 1.2 GB of it, so the run above sets 8 GiB ([the quickjs sandbox](https://tinyactors.dev/guides/quickjs/index.md#the-limits)).

A chart's scripts add nothing per actor. Their functions are the chart's, made once, and a top-level `var` is one more variable ([Writing charts](https://tinyactors.dev/guides/charts/index.md#scripts)).

<a id="work-does-not-grow-with-the-population"></a>

## Check that costs hold as actors double

The scaling program, `tools/scale.mjs`, times every operation on N actors and on 2N, each host in its own process. It fails an operation that costs more than 1.5 times as much at 2N.

An operation on one actor is timed per call, and one on the whole population per actor.

In the first run most actors are hibernated in SQLite, and one in a hundred runs an invoked service. In the second every actor is resident.

Every operation passed. Each cell is the cost at N, then at 2N, in microseconds, and their ratio.

| operation | unit | node, native | node, quickjs | bun, native | bun, quickjs |
|---|---|---|---|---|---|
| populate (spawn and hibernate) | µs per actor | 10.88 → 10.56 (0.97) | 7.43 → 7.56 (1.02) | 7.52 → 7.81 (1.04) | 6.26 → 6.02 (0.96) |
| spawn | µs per call | 5.25 → 5.19 (0.99) | 2.62 → 2.60 (0.99) | 4.11 → 4.16 (1.01) | 2.43 → 2.45 (1.01) |
| terminate | µs per call | 0.867 → 0.786 (0.91) | 0.571 → 0.575 (1.01) | 0.952 → 0.893 (0.94) | 0.751 → 0.720 (0.96) |
| send | µs per call | 3.79 → 3.64 (0.96) | 1.90 → 1.77 (0.93) | 3.19 → 3.00 (0.94) | 2.12 → 2.06 (0.97) |
| sendMany | µs per event | 3.98 → 3.86 (0.97) | 2.18 → 2.04 (0.93) | 2.87 → 2.83 (0.99) | 2.01 → 1.85 (0.92) |
| send (hibernated: woken from the store) | µs per call | 32.27 → 27.93 (0.87) | 28.29 → 27.92 (0.99) | 24.71 → 25.96 (1.05) | 33.43 → 33.10 (0.99) |
| hibernate | µs per call | 5.41 → 4.93 (0.91) | 4.21 → 4.17 (0.99) | 3.56 → 3.44 (0.97) | 4.07 → 3.86 (0.95) |
| wake | µs per call | 6.94 → 6.38 (0.92) | 3.90 → 3.69 (0.95) | 6.91 → 7.27 (1.05) | 4.19 → 4.43 (1.06) |
| actor (by name) | µs per call | 3.96 → 4.01 (1.01) | 3.96 → 3.88 (0.98) | 1.32 → 1.20 (0.91) | 1.13 → 1.12 (0.99) |
| actorState | µs per call | 4.26 → 4.01 (0.94) | 4.15 → 4.11 (0.99) | 3.90 → 3.73 (0.96) | 3.66 → 3.58 (0.98) |
| hostStats | µs per call | 25.65 → 25.05 (0.98) | 24.49 → 23.49 (0.96) | 19.35 → 19.60 (1.01) | 18.96 → 18.23 (0.96) |
| I/O processor round trip | µs per call | 10.11 → 10.16 (1.01) | 6.81 → 6.73 (0.99) | 7.09 → 6.87 (0.97) | 5.77 → 5.54 (0.96) |
| transport round trip | µs per call | 30.55 → 31.31 (1.02) | 25.92 → 26.36 (1.02) | 25.19 → 26.15 (1.04) | 22.05 → 23.06 (1.05) |
| delayed send and its timer | µs per call | 6.28 → 6.19 (0.98) | 3.09 → 3.01 (0.97) | 4.95 → 5.15 (1.04) | 3.35 → 3.37 (1.01) |
| invoked service round trip | µs per call | 13.52 → 13.93 (1.03) | 10.33 → 10.12 (0.98) | 10.13 → 10.66 (1.05) | 9.15 → 9.79 (1.07) |
| sweep (every actor) | µs per actor | 0.006 → 0.005 (0.87) | 0.006 → 0.005 (0.87) | 0.006 → 0.006 (0.95) | 0.006 → 0.006 (0.91) |
| migrate (one session) | µs per call | 24.09 → 25.32 (1.05) | 16.51 → 16.95 (1.03) | 35.62 → 20.64 (0.58) | 16.90 → 16.30 (0.96) |
| snapshot | µs per actor | 0.100 → 0.092 (0.92) | 0.079 → 0.084 (1.07) | 0.081 → 0.087 (1.08) | 0.101 → 0.092 (0.92) |
| restore (and its first turn) | µs per actor | 0.291 → 0.298 (1.02) | 0.217 → 0.215 (0.99) | 0.223 → 0.217 (0.98) | 0.198 → 0.192 (0.97) |
| close | µs per actor | 0.114 → 0.128 (1.13) | 0.120 → 0.157 (1.31) | 0.126 → 0.127 (1.01) | 0.120 → 0.139 (1.16) |

Hibernated: 2,000,000 and 4,000,000 actors, at most 1000 counters resident, 20,000 and 40,000 actors each running an invoked service. One run, alone on the machine, 2026-11-20: an Intel Xeon at 2.10 GHz, 4 CPUs, 16 GiB, Linux; Node.js 22.23.5 and Bun 1.4.1.

| operation | unit | node, native | node, quickjs | bun, native | bun, quickjs |
|---|---|---|---|---|---|
| populate (spawn, the segment growing) | µs per actor | 5.42 → 5.52 (1.02) | 4.28 → 3.85 (0.90) | 4.23 → 4.14 (0.98) | 3.76 → 3.63 (0.97) |
| spawn | µs per call | 4.84 → 4.86 (1.00) | 2.47 → 2.34 (0.95) | 4.06 → 4.14 (1.02) | 2.47 → 2.36 (0.95) |
| send | µs per call | 5.82 → 5.89 (1.01) | 3.88 → 3.97 (1.02) | 4.88 → 4.90 (1.00) | 4.01 → 4.19 (1.05) |
| hibernate | µs per call | 6.29 → 6.54 (1.04) | 6.89 → 6.31 (0.92) | 4.54 → 4.92 (1.09) | 6.78 → 6.85 (1.01) |
| wake | µs per call | 7.48 → 7.37 (0.99) | 2.85 → 2.59 (0.91) | 5.08 → 5.45 (1.07) | 3.19 → 3.14 (0.98) |
| snapshot | µs per actor | 4.49 → 4.23 (0.94) | 5.51 → 5.65 (1.03) | 3.50 → 3.20 (0.91) | 6.11 → 5.59 (0.92) |
| migrate (a chart, every actor) | µs per actor | 17.99 → 17.80 (0.99) | 11.44 → 10.58 (0.93) | 14.38 → 14.06 (0.98) | 11.59 → 9.99 (0.86) |
| restore (and its first turn) | µs per actor | 13.04 → 12.69 (0.97) | 4.35 → 4.96 (1.14) | 7.32 → 6.98 (0.95) | 4.57 → 4.79 (1.05) |
| close | µs per actor | 0.310 → 0.309 (0.99) | 1.71 → 1.53 (0.90) | 0.296 → 0.323 (1.09) | 1.74 → 1.56 (0.90) |

All resident: 1,000,000 and 2,000,000 actors, the same run.

Two million resident actors take up to 5.2 GiB on quickjs, the most of any run. So four million resident do not fit in 16 GiB beside the scenario's other work.

| runtime, datamodel | hibernated | all resident |
|---|---|---|
| node, native | 1733 / 2579 | 1706 / 3007 |
| node, quickjs | 1773 / 3382 | 2555 / 5274 |
| bun, native | 1680 / 3251 | 2271 / 3828 |
| bun, quickjs | 1345 / 2554 | 2517 / 5278 |

Peak resident memory at N and 2N, in MiB, the same run.

`mise run scale` in `tinyactors-napi` writes every operation and sample to `build/scale-*.json`. This run's files are in [tools/results/2026-11-20](https://github.com/tinyactors-dev/tinyactors/tree/main/tinyactors-napi/tools/results/2026-11-20).

The repository's rules for [published numbers](https://github.com/tinyactors-dev/tinyactors/blob/main/AGENTS.md#published-numbers) say when to run it again, before each release among them.
