Espandere e gestire la memoria lineare
Imparate come cresce su richiesta la memoria lineare WASM, come dimensionarla e come gestire le allocazioni in sicurezza da JavaScript e dal modulo.
Espandere e gestire la memoria lineare è una lezione WebAssembly (WASM) for High Performance Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebAssembly (WASM) for High Performance Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara WebAssembly (WASM) for High Performance Apps con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Espandere e gestire la memoria lineare» è gratuita?
Sì — il testo completo di «Espandere e gestire la memoria lineare» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebAssembly (WASM) for High Performance Apps, passa a CoddyKit PRO. Il corso WebAssembly (WASM) for High Performance Apps include 4 lezioni in totale.
Cosa imparerò in «Espandere e gestire la memoria lineare»?
Imparate come cresce su richiesta la memoria lineare WASM, come dimensionarla e come gestire le allocazioni in sicurezza da JavaScript e dal modulo. Eserciti WebAssembly (WASM) for High Performance Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebAssembly (WASM) for High Performance Apps?
Non è richiesta alcuna esperienza precedente. WebAssembly (WASM) for High Performance Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Espandere e gestire la memoria lineare»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebAssembly (WASM) for High Performance Apps?
Sì. Ogni lezione WebAssembly (WASM) for High Performance Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Passare strutture dati complesse
- Modello e gestione della memoria WASM
- Memoria condivisa e atomiche
- Espandere e gestire la memoria lineare