
# Memory

**`memoryMiB` is the whole VM's memory**, fixed when it boots. The host commits a page when the
guest first touches it and never gets it back, and the guest's page cache keeps what it reads, so
over a long run the VM's share grows to `memoryMiB`. In a browser that is, in effect, the bound of
the tab's memory for the VM.

## What an image needs

A build with a probe starts the image and measures the peak of MemTotal - MemAvailable until it
is ready, over all of its starts, and records `memoryMiB`: the peak rounded up to 32 MiB, twice
that, plus 32 MiB, so that the same build records the same value. Measured
([`spikes/25-memory/repeat-build.ts`](https://github.com/OskarLebuda/untainer/blob/main/spikes/25-memory/repeat-build.ts)): node:24-alpine **96 MiB** and postgres:17-alpine
**224 MiB** in every build, ten each on macOS and three each on Linux arm64 and x86_64.

A runtime given images up front boots with the sum of theirs; `createRuntime({ memoryMiB })`
overrides it.

That is a profile of the **start**. A database that does work needs more: postgres:17-alpine
profiled with a script of queries (`--profile-script`) records 352 MiB instead of 224. Build an
application that will be busy with a script that makes it busy.

## One pool

There is no limit per container: every container draws on the VM's memory, and when it runs out
the guest kernel kills the **largest process in the whole VM**, which may be in another container
than the one that allocated. untainer tells whose process it was from the kernel's console line,
without asking the guest: `wait()` returns `oomKilled: true` with the line, and `ready()` fails
with `ERR_OOM`.

## When memory runs out

A guest short of memory could stall for minutes while its kernel tries to reclaim some. Two
things keep that from happening: the runtime keeps a reserve free for the guest kernel, and a
small **memory guard** in the guest (as earlyoom on a Linux host) has the kernel kill the largest
process as soon as memory stays short. What you see is the kill, reported as above.

Two things to plan for:

- **A runtime can end.** A guest that stops answering after running out of memory ends its
  runtime: every container's `wait()` gets `ERR_OOM` (or `ERR_VM`), and every later call says to
  create a new runtime. Its kernel may still run, but its connection to the host (vsock) does not
  come back once memory has stayed at zero for a moment: the kill alone does not bring it back,
  in a worker or not. It happens below untainer, so the way out is memory: give the VM room for
  its largest process (`memoryMiB`), or make that process smaller. In a VM of 96 MiB with a
  process that grows until it is killed, the connection was lost in 11 of 90 runs on Linux x86_64
  under emulation and in none of 60 on macOS
  ([`docs/findings.md`](https://github.com/OskarLebuda/untainer/blob/main/docs/findings.md),
  "The connection that does not come back").
- **Calls wait about 2 s after a kill.** From the kernel's first line about memory until it has
  been quiet for 2 s, the runtime starts no new command in the guest (`exec()`, `spawn()`,
  `run()`): a call made then takes about 2 s longer, counted in its timeout.

How these were measured, and the numbers, are in [`docs/runtime.md`](https://github.com/OskarLebuda/untainer/blob/main/docs/runtime.md),
"Memory", and [`spikes/29-oom-hang/`](https://github.com/OskarLebuda/untainer/tree/main/spikes/29-oom-hang).
