تنمية الذاكرة الخطية وإدارتها
تعلّم كيف تنمو الذاكرة الخطية في WASM عند الطلب، وكيفية تحديد حجمها وإدارة التخصيص بأمان من JavaScript والوحدة.
تنمية الذاكرة الخطية وإدارتها درس مجاني في WebAssembly (WASM) for High Performance Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.
الأسئلة الشائعة
هل درس «تنمية الذاكرة الخطية وإدارتها» مجاني؟
نعم — نص درس «تنمية الذاكرة الخطية وإدارتها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebAssembly (WASM) for High Performance Apps، انتقل إلى CoddyKit PRO. تتضمن دورة WebAssembly (WASM) for High Performance Apps 4 دروس في المجموع.
ماذا ستتعلم في «تنمية الذاكرة الخطية وإدارتها»؟
تعلّم كيف تنمو الذاكرة الخطية في WASM عند الطلب، وكيفية تحديد حجمها وإدارة التخصيص بأمان من JavaScript والوحدة. تتمرن على WebAssembly (WASM) for High Performance Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebAssembly (WASM) for High Performance Apps؟
لا تُشترط خبرة سابقة. WebAssembly (WASM) for High Performance Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تنمية الذاكرة الخطية وإدارتها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebAssembly (WASM) for High Performance Apps هذا؟
نعم. كل درس في WebAssembly (WASM) for High Performance Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تمرير بنى البيانات المعقدة
- نموذج ذاكرة WASM وإدارتها
- الذاكرة المشتركة والعمليات الذرية
- تنمية الذاكرة الخطية وإدارتها