0Pricing
WebAssembly (WASM) for High Performance Apps · 강의

선형 메모리 확장 및 관리

WASM 선형 메모리가 필요에 따라 확장되는 방식과 크기를 설정하는 방법을 익히고, JavaScript와 모듈에서 메모리 할당을 안전하게 관리합니다.

선형 메모리 확장 및 관리은(는) CoddyKit의 무료 WebAssembly (WASM) for High Performance Apps 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 WebAssembly (WASM) for High Performance Apps 강의 전체를 잠금 해제할 수 있습니다. WebAssembly (WASM) for High Performance Apps 강의에는 총 4개의 강의가 포함되어 있습니다.

“선형 메모리 확장 및 관리”에서 뭘 배우나요?

WASM 선형 메모리가 필요에 따라 확장되는 방식과 크기를 설정하는 방법을 익히고, JavaScript와 모듈에서 메모리 할당을 안전하게 관리합니다. 브라우저에서 직접 실행하는 실습 코드로 WebAssembly (WASM) for High Performance Apps을(를) 배우며, 24/7 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(으)로 돌아가기