0Pricing
WebAssembly (WASM) for High Performance Apps · レッスン

線形メモリの拡張と管理

WASMの線形メモリが必要に応じて拡張される仕組み、サイズの決め方、JavaScriptとモジュールから安全にアロケーションを管理する方法を学びます。

「線形メモリの拡張と管理」はCoddyKit上の無料WebAssembly (WASM) for High Performance Appsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebAssembly (WASM) for High Performance Apps学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebAssembly (WASM) for High Performance Appsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

よくある質問

「線形メモリの拡張と管理」レッスンは無料ですか?

はい。「線形メモリの拡張と管理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebAssembly (WASM) for High Performance Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebAssembly (WASM) for High Performance Appsコースには全4レッスンが含まれています。

「線形メモリの拡張と管理」で何を学びますか?

WASMの線形メモリが必要に応じて拡張される仕組み、サイズの決め方、JavaScriptとモジュールから安全にアロケーションを管理する方法を学びます。 ブラウザで直接実行するハンズオンコードでWebAssembly (WASM) for High Performance Appsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

WebAssembly (WASM) for High Performance Appsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのWebAssembly (WASM) for High Performance Appsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「線形メモリの拡張と管理」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このWebAssembly (WASM) for High Performance Appsレッスンでコードを書いて実行できますか?

はい。すべてのWebAssembly (WASM) for High Performance Appsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 複雑なデータ構造の受け渡し
  2. WASMのメモリモデルと管理
  3. 共有メモリとアトミック操作
  4. 線形メモリの拡張と管理
← WebAssembly (WASM) for High Performance Appsに戻る