Linearen Speicher erweitern und verwalten
Lernen Sie, wie der lineare Speicher von WASM bei Bedarf wächst, wie Sie ihn dimensionieren und wie Sie die Allokation sicher aus JavaScript und dem Modul heraus verwalten.
Linearen Speicher erweitern und verwalten ist eine kostenlose WebAssembly (WASM) for High Performance Apps-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des WebAssembly (WASM) for High Performance Apps-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der WebAssembly (WASM) for High Performance Apps-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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 pageBuffers 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 pageAllocators 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/_freecarve the space - Free what you allocate; size initial/max thoughtfully
Careful memory management keeps high-performance WASM apps fast and leak-free.
Häufig gestellte Fragen
Ist die Lektion „Linearen Speicher erweitern und verwalten“ kostenlos?
Ja — der vollständige Text von „Linearen Speicher erweitern und verwalten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des WebAssembly (WASM) for High Performance Apps-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der WebAssembly (WASM) for High Performance Apps-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Linearen Speicher erweitern und verwalten“?
Lernen Sie, wie der lineare Speicher von WASM bei Bedarf wächst, wie Sie ihn dimensionieren und wie Sie die Allokation sicher aus JavaScript und dem Modul heraus verwalten. Du übst WebAssembly (WASM) for High Performance Apps mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um WebAssembly (WASM) for High Performance Apps zu starten?
Keine Vorkenntnisse erforderlich. WebAssembly (WASM) for High Performance Apps auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Linearen Speicher erweitern und verwalten“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser WebAssembly (WASM) for High Performance Apps-Lektion Code schreiben und ausführen?
Ja. Jede WebAssembly (WASM) for High Performance Apps-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Komplexe Datenstrukturen übergeben
- WASM-Speichermodell und -verwaltung
- Gemeinsamer Speicher und Atomics
- Linearen Speicher erweitern und verwalten