增长与管理线性内存
学习 WASM 线性内存如何按需增长、如何确定其大小,以及如何从 JavaScript 和模块中安全管理内存分配。
增长与管理线性内存 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 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.
用 AI 导师学习 WebAssembly (WASM) for High Performance Apps — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「增长与管理线性内存」课时是免费的吗?
是的 — 「增长与管理线性内存」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。
「增长与管理线性内存」这节课中我会学到什么?
学习 WASM 线性内存如何按需增长、如何确定其大小,以及如何从 JavaScript 和模块中安全管理内存分配。 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 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 反馈 — 无需本地设置。
此课程中的所有课时
- 传递复杂数据结构
- WASM 内存模型与管理
- 共享内存与原子操作
- 增长与管理线性内存