WASM Memory Model & Management
Understand WebAssembly's linear memory model and how memory is allocated, accessed, and managed within WASM modules.
WASM Linear Memory: The Basics
WebAssembly uses a linear memory model. Think of it as a single, large, contiguous array of bytes, similar to how traditional programs manage memory.
- This memory is separate from JavaScript's memory.
- It's accessed by WASM modules as a flat address space, starting from address 0.
- All data (integers, floats, strings, arrays) lives within this single memory block.
The `WebAssembly.Memory` Object
In JavaScript, WASM memory is represented by the WebAssembly.Memory object. This object holds the actual memory buffer.
You can create it:
- When instantiating a WASM module, it can declare and create its own memory.
- You can also pass an existing
WebAssembly.Memoryinstance from JavaScript to the module.
const memory = new WebAssembly.Memory({
initial: 1, // Start with 1 page (64KB)
maximum: 10 // Max allowed 10 pages
});
// This 'memory' object is then passed to the
// WASM module during instantiation.All lessons in this course
- Passing Complex Data Structures
- WASM Memory Model & Management
- Shared Memory & Atomics
- Growing & Managing Linear Memory