Skip to content
WASM Analyzer LogoWASM Analyzer LogoWASM Analyzer
GitHub

Memories

In WebAssembly, a memory is a resizable ArrayBuffer or SharedArrayBuffer. In other terms, an ArrayBuffer represents a binary data buffer, or byte array. A SharedArrayBuffer is the same thing as an ArrayBuffer, but not transferable between contexts, and set up in a way where it can be used for views in shared memory, unlike an ArrayBuffer.

Memories can be both imported into, or exported from, a module (see the Imports section of our documentation for an example of passing imports, like a table, into a module).





Examples


Creating a memory can look like this:

const aMemory = new WebAssembly.Memory({
  initial: 10, // starting, and minimum number of pages
  maximum: 100, // maximum number of pages
});


Accessing an exported memory looks something like this:

WebAssembly.instantiateStreaming(fetch("module.wasm")).then((obj) => {
  const memoryContents = new Uint32Array(obj.instance.exports.memory.buffer); // setting the memory contents to a variable
  const firstValue = values[0]; // setting the contents from the first position in the memory to a variable
});




Additional reading


You can read more about memory in Mozilla’s Wasm documentation here, read more about ArrayBuffers here and SharedArrayBuffers here.