WebAssembly linear memory is one big, growable array of bytes that a WebAssembly module reads and writes by address. It is measured in pages of 64 KiB, it starts at a size the module declares, and it can only get bigger, through an instruction called memory.grow. There is no garbage collector inside it. The language that compiled the module decides where every string, list and object goes, and when (or whether) that space is reused.

If you come from JavaScript, Python, Java or C#, that is the surprise. You never think about where an object lives. In linear memory, someone has to. Below is how that works, and then how Clean, the language I am building, handles it, weak parts included.

One array of bytes, measured in pages

MDN describes a WebAssembly.Memory as "a resizable ArrayBuffer or SharedArrayBuffer that holds raw bytes of memory accessed by a WebAssembly.Instance" (MDN). Think of it as a numbered row of boxes, each holding one byte. Address 0 is the first box. A pointer is just a number: the index of a box.

The size is counted in pages, and each page is 64 KiB. A module declares an initial size and, usually, a maximum. MDN's own example creates a memory with 10 pages (640 KiB) that may grow to 100 pages (6.4 MiB).

To get more room, the module runs memory.grow with a number of pages. MDN explains the result: the instruction returns "the previous size of memory (in pages)" if it worked, "or -1 if the operation failed" (MDN, grow). It does not crash on failure. The code has to check for -1 itself.

Two details catch people out. Memory never shrinks; there is no memory.shrink. And when memory grows, growing "detaches the previous buffer", so JavaScript typed arrays over the old buffer must be created again.

Where the stack and the heap live

In a native program you have a stack (for function calls and local variables) and a heap (for everything that lives longer). WebAssembly splits this in an unusual way.

The real call stack, with return addresses and plain local values, lives outside linear memory. The WebAssembly security document calls it "a protected call stack that is invulnerable to buffer overflows in the module heap" (webassembly.org). The module cannot even read it by address.

But languages like C, C++ and Rust sometimes need a local variable to have an address, for example to pass a pointer to it. So their compilers keep a second, "shadow" stack inside linear memory. Emscripten reserves 64 KiB for it by default, and its documentation warns: "There is no way to enlarge the stack, so this value must be large enough for the program's requirements" (Emscripten settings).

Allocators: who decides where things go

WebAssembly gives you bytes and the memory.grow instruction. It does not give you malloc or free. Each language ships its own allocator, compiled into the module.

  • Bump allocator. The simplest design. Keep one pointer to the first free byte. To allocate, return that pointer and move it forward. It is very fast and very small. It cannot free a single block; it can only move the pointer back to an earlier point and drop everything after it at once.
  • dlmalloc. A classic general-purpose allocator with a free list, so freed blocks are reused. Rust's dlmalloc crate says its "primary purpose" is to serve "as the default memory allocator for the wasm32-unknown-unknown target in the standard library" (docs.rs). Emscripten also uses dlmalloc by default, and offers emmalloc ("a simple and compact malloc") and mimalloc as options.
  • wee_alloc. A tiny allocator that many older Rust and Wasm tutorials still recommend. Don't. The RustSec advisory RUSTSEC-2022-0054 marks it unmaintained, notes open "memory leaks", and suggests switching "to the default Rust standard allocator on wasm32 targets" (RustSec).

The tradeoff: a smarter allocator reuses memory better but adds code size and work. A simpler one is smaller and faster but wastes space unless you can drop whole regions at once.

Why linear memory is safe for the host

If a module can write to any address, why is it safe to run untrusted Wasm in a browser? Because "any address" means any address inside its own array. The security document says: "Accesses to linear memory are bounds-checked at the region level, potentially resulting in a trap at runtime." A read or write past the end stops the module. It cannot touch the browser's memory, other tabs, or the host's data.

That protects the host, not your program. The same document is honest about it: "Data stored in linear memory can overwrite adjacent objects, since bounds checking is performed at linear memory region granularity." A buffer overflow in C compiled to Wasm can still corrupt the C program's own data. The sandbox keeps the damage inside; it does not prevent it.

memory64 and multi-memory

Classic Wasm memory uses 32-bit addresses, which caps it at 4 GiB. WebAssembly 3.0, completed in September 2025, lets memories "use i64 as their address type instead of just i32". In theory that is 16 exabytes, but "on the web, a 64-bit memory is limited to 16 gigabytes" (Wasm 3.0 announcement).

Wasm 3.0 also allows multiple memories: "A single module can now declare (define or import) multiple memories and directly access them, including directly copying data between them."

Support is not universal yet. According to the WebAssembly feature status table, memory64 ships in Chrome 133, Firefox 134, Node.js 24 and Wasmtime 30, but in Safari it is still behind a flag. Multi-memory ships in Chrome 120, Firefox 125 and Wasmtime 15, and the table does not list Safari support. If you target every browser, plan on one 32-bit memory for now.

Wasm 3.0 GC: the part that will feel familiar

The biggest change for developers from managed languages is garbage collection. Wasm 3.0 adds "a new (and separate) form of storage that is automatically managed by the Wasm runtime via a garbage collector". A compiler can describe its objects as struct and array types, and the engine allocates and collects them, the same engine that collects JavaScript objects in a browser.

Note the word "separate". GC objects do not live in linear memory, and linear memory is unchanged. A module can use both. The announcement lists Java, OCaml, Scala, Kotlin, Scheme and Dart as languages that already use the GC feature. For them it means no garbage collector shipped inside every module and smaller downloads. GC is widely available: Chrome 119, Firefox 120, Safari 18.2 and Node.js 22 all support it, according to the feature table.

For C, C++ and Rust, nothing changes. They still manage linear memory themselves.

How Clean uses linear memory

Clean compiles straight to WebAssembly, and today it uses one 32-bit linear memory with no garbage collector. This section describes the current compiler (version 0.33) and follows our memory model specification. From the bottom up, the memory looks like this:

RegionWhat is there
0 to 4 KBUnused, all zeros. Address 0 is the null pointer, and its zero bytes read as an empty string.
From 4 KBEvery string literal in the program, stored once even if it appears many times.
Next 64 KBA reserved "transient arena" for short-lived values inside loops.
RestThe main heap, which grows upward with memory.grow.

The start of the heap depends on how much literal text your program has, so the compiler exports it as a global named __heap_ptr. Hosts read the start from there; they never guess.

Strings are stored with a 4-byte length in front, followed by the UTF-8 bytes, with no terminating zero. I covered why in How strings work in WebAssembly. Here is a small program:

start:
	string greeting = "Hello"
	string name = "Ada"
	string message = greeting + ", " + name
	print(message)
	print(message.length().toString())

"Hello", ", " and "Ada" are literals, so they sit in the literal area from the moment the module loads. message is built while the program runs, so it goes on the heap: 4 bytes of length plus 9 bytes of text, rounded up to 16, because every heap block is aligned to 8 bytes.

The allocator is a bump allocator. Every allocation moves __heap_ptr forward. If the next block does not fit, it calls memory.grow for the missing pages, and if the grow is refused, it returns 0 instead of crashing. There is no free list, no reference counting and no garbage collector. The module exports a free function, but it does nothing.

Memory comes back in whole regions instead. The module exports scope_push, which returns the current heap position, and scope_pop, which moves the pointer back to that position and drops everything allocated since then in one step. On the server, our runtime goes further and gives each request a fresh instance, so whatever a request allocated is gone when it ends.

Loops are where a bump allocator hurts most, because every pass can create new temporary strings. That is what the 64 KB transient arena is for:

start:
	list<integer> scores = [90, 75, 82]
	scores.add(68)
	string line = ""
	iterate s in scores
		line = line + s.toString() + " "
	print(line)

Each pass turns a number into text and builds a longer string. When the compiler rewrites a loop like this into an accumulator loop, the in-between values go into the transient arena, which has its own pointer and is reset on every pass, so they don't pile up on the main heap. If the arena fills, allocations fall back to the main heap. The list itself is one heap block: a 16-byte header (count, capacity, type and behaviour flags) followed by 4 bytes per integer.

Memory size is set by tiers. The default "standard" tier starts at 32 pages (2 MB) and may grow to 512 pages (32 MB). Smaller and larger tiers exist, from "embedded" (1 MB maximum) to "canvas" (16 MB to start, 64 MB maximum), and you pick one with the --memory-tier compiler flag.

Where Clean's approach is weak

I chose a bump allocator because it is small, fast and easy to get right, and it fits the server case well: allocate during a request, drop it all at the end. But it has real limits.

  • Long-running programs only grow. A browser page, a game or a desktop app never ends a "request". Without a reset point, memory keeps climbing until it hits the tier's maximum. Our own memory policy says that for page-lifetime targets "a general-purpose allocator with free is eventually needed". We don't have one yet.
  • Resets are all or nothing. After scope_pop, every value allocated since the save point is invalid. Host code that keeps a pointer past that point reads garbage. The compiler handles this for normal Clean code, but host and plugin authors must be careful.
  • Hard ceilings. In the standard tier, a program that needs more than 32 MB cannot get it, and a failed allocation returns 0, which shows up as an error, not as a bigger heap.
  • No Wasm 3.0 memory features yet. Clean does not use WasmGC, memory64 or multiple memories.

Clean is in Beta, built by a solo developer, and a new compiler is in development, so the memory design may change. If you need fine control of memory today, Rust or C++ give you that. If you want memory you never think about, a language that uses WasmGC, such as Kotlin or Dart, is closer to what you know.

Next step

If you want to see these ideas in a working language, install Clean and try the first tutorial. To go deeper on how text is laid out in these bytes, read How strings work in WebAssembly.

Sources