Memory Growth
A memory starts at its initial size, but programs often don't know in advance how much they'll need. The
memory.grow instruction adds pages at runtime.
| Instruction | Type | Meaning |
|---|---|---|
memory.size |
[] → [i32] |
current size in pages |
memory.grow |
[i32] → [i32] |
add N pages; returns the old size, or -1 on failure |
Growing memory
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(call $log (memory.size))
(call $log (memory.grow (i32.const 2))) ;; returns the previous size: 1
(call $log (memory.size)) ;; now 3 pages
(call $log (memory.grow (i32.const 0))))) ;; growing by 0 is a way to query the size1
1
3
3
New pages are zero-filled, and existing contents are preserved. Memory never shrinks: there is no
memory.shrink. Allocators reuse freed space instead.
The maximum and failure
If the memory declares a maximum, growing beyond it fails. Failure is not a trap: memory.grow returns -1 and the
program decides what to do (C's malloc returns NULL, Rust panics with "out of memory").
(module
(import "console" "log" (func $log (param i32)))
(memory 1 4) ;; at most 4 pages
(func (export "main")
(call $log (memory.grow (i32.const 2))) ;; 1 -> 3: ok
(call $log (memory.grow (i32.const 2))) ;; 3 -> 5: over the maximum
(call $log (memory.size))
(call $log (memory.grow (i32.const 1))) ;; 3 -> 4: ok
(call $log (memory.size))))1
-1
3
3
4
Without a maximum, the limit is the engine's own (for 32-bit memories at most 65,536 pages = 4 GiB; browsers may allow less, and the allocation can fail if the device is low on memory).
Growing makes new addresses valid
Before growth, an address past the end traps. After growth, it's ordinary memory:
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func $try_store (param $addr i32) (param $value i32) (result i32)
;; only store if the address fits, else return 0
(if (result i32) (i32.le_u (i32.add (local.get $addr) (i32.const 4))
(i32.mul (memory.size) (i32.const 65536)))
(then (i32.store (local.get $addr) (local.get $value)) (i32.const 1))
(else (i32.const 0))))
(func (export "main")
(call $log (call $try_store (i32.const 70000) (i32.const 5))) ;; doesn't fit in 1 page
(drop (memory.grow (i32.const 1)))
(call $log (call $try_store (i32.const 70000) (i32.const 5))) ;; fits in 2 pages
(call $log (i32.load (i32.const 70000)))))0
1
5
A bump allocator
Real programs need an allocator that hands out chunks of memory. The simplest one is a bump allocator: keep a pointer to the next free byte and move it forward on every allocation, growing memory when needed.
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(global $heap_top (mut i32) (i32.const 1024)) ;; first 1 KiB reserved
;; returns the address of $size fresh bytes (8-byte aligned), or 0 on failure
(func $alloc (param $size i32) (result i32)
(local $ptr i32) (local $new_top i32) (local $needed i32)
(local.set $ptr (global.get $heap_top))
(local.set $new_top
(i32.and (i32.add (i32.add (local.get $ptr) (local.get $size)) (i32.const 7))
(i32.const -8))) ;; round up to 8
;; pages needed beyond the current size
(local.set $needed
(i32.sub (i32.shr_u (i32.add (local.get $new_top) (i32.const 65535)) (i32.const 16))
(memory.size)))
(if (i32.gt_s (local.get $needed) (i32.const 0))
(then
(if (i32.eq (memory.grow (local.get $needed)) (i32.const -1))
(then (return (i32.const 0))))))
(global.set $heap_top (local.get $new_top))
(local.get $ptr))
(func (export "main")
(call $log (call $alloc (i32.const 10))) ;; 1024
(call $log (call $alloc (i32.const 3))) ;; 1040 (1034 rounded up to 8)
(call $log (call $alloc (i32.const 100000))) ;; forces growth
(call $log (memory.size))
(call $log (global.get $heap_top))))1024
1040
1048
2
101048
Bump allocators are fast but can't free individual blocks. Production toolchains ship real allocators (dlmalloc in
Emscripten, Rust's default allocator, or the tiny wee_alloc/talc) compiled into the module.
Growth and JavaScript views
When memory grows, the old ArrayBuffer that JavaScript holds is detached and a new, larger one replaces it.
Typed arrays made from the old buffer become length 0. Always re-create views from memory.buffer after any call
that might allocate:
let bytes = new Uint8Array(instance.exports.memory.buffer);
instance.exports.allocateLots(); // grows memory internally
console.log(bytes.length); // 0: the old view is detached
bytes = new Uint8Array(instance.exports.memory.buffer); // fresh viewThis is one of the most common bugs in JS/Wasm glue code; you'll see it run in Memory, Table & Global from JS.
Test yourself
What does memory.grow return on success?
- The new size in pages
- The old size in pages
- 1
- The address of the new space in bytes
What happens when memory.grow would exceed the declared maximum?
- It traps
- It returns -1 and memory is unchanged
- It grows to the maximum
- The maximum is ignored
Why must JavaScript re-create typed array views after memory grows?
- The old buffer is detached and the views become empty
- The bytes are moved to random addresses
- Typed arrays become read-only
- They don't need to