0Pricing
WebAssembly (WASM) for High Performance Apps · Lesson

Growing & Managing Linear Memory

Learn how WASM linear memory grows on demand, how to size it, and how to manage allocation safely from JavaScript and the module.

Growing & Managing Linear Memory is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

One Big Array

WASM stores all its data in a single, contiguous block called linear memory, exposed to JS as a growable ArrayBuffer.

Understanding how it grows and how to manage it is key to high-performance modules.

Memory Is Page-Based

Linear memory is measured in pages of 64 KiB each.

  • 1 page = 65536 bytes
  • Memory grows in whole pages, never partial

Declaring Initial and Max Size

When creating memory you set an initial and optional maximum number of pages.

const memory = new WebAssembly.Memory({
  initial: 2,   // 128 KiB
  maximum: 100  // up to ~6.4 MiB
});

Growing Memory

Call grow(n) to add n pages. It returns the previous size, or throws if it would exceed the maximum.

const prevPages = memory.grow(1); // add one 64 KiB page

Buffers Become Detached

Important: growing memory detaches the old ArrayBuffer. Any typed-array view you held is now invalid, re-create it after growth.

memory.grow(1);
// old view is stale; make a fresh one
const view = new Uint8Array(memory.buffer);

Growing From Inside WASM

WASM code grows its own memory with the memory.grow instruction; allocators like Rust's do this automatically when the heap fills.

(memory.grow (i32.const 1)) ;; grow by 1 page

Allocators on Top of Memory

Languages provide an allocator (malloc, Rust's global allocator) that carves the linear memory into objects.

You rarely manage raw offsets, you let the allocator and bindings do it.

Allocating From JS

Emscripten exposes _malloc and _free so JS can reserve space in WASM memory for data to pass in.

const ptr = Module._malloc(1024);
// ... use the buffer at ptr ...
Module._free(ptr);

Avoiding Leaks

Memory you allocate must be freed, WASM has no garbage collector for its linear memory. Forgetting free leaks until the module is discarded.

Sizing Strategy

Tune memory for your workload:

  • Start with enough initial pages to avoid early growth
  • Set a maximum to cap runaway usage
  • Remember each grow detaches views, batch growth when possible

Best Practices Summary

To manage linear memory well:

  • Think in 64 KiB pages
  • Re-create views after every grow
  • Pair every alloc with a free
  • Pick sensible initial and maximum sizes

Quick Check

What must you do immediately after calling memory.grow()?

Recap

You learned to manage WASM linear memory:

  • Memory grows in 64 KiB pages via grow()
  • Growth detaches buffers, refresh your views
  • Allocators and _malloc/_free carve the space
  • Free what you allocate; size initial/max thoughtfully

Careful memory management keeps high-performance WASM apps fast and leak-free.

Frequently asked questions

Is the “Growing & Managing Linear Memory” lesson free?

Yes — the full text of “Growing & Managing Linear Memory” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.

What will I learn in “Growing & Managing Linear Memory”?

Learn how WASM linear memory grows on demand, how to size it, and how to manage allocation safely from JavaScript and the module. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start WebAssembly (WASM) for High Performance Apps?

No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Growing & Managing Linear Memory” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?

Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Passing Complex Data Structures
  2. WASM Memory Model & Management
  3. Shared Memory & Atomics
  4. Growing & Managing Linear Memory
← Back to WebAssembly (WASM) for High Performance Apps