Fundamentals 6 min read

A Crash Course in Memory Management: ArrayBuffer, SharedArrayBuffer, and Manual vs Automatic Management in JavaScript

This article explains the fundamentals of memory management, illustrating why ArrayBuffer and SharedArrayBuffer were added to JavaScript, how automatic garbage collection works, and what manual memory handling with functions like malloc and free entails, especially when using WebAssembly and C.

Hujiang Technology
Hujiang Technology
Hujiang Technology
A Crash Course in Memory Management: ArrayBuffer, SharedArrayBuffer, and Manual vs Automatic Management in JavaScript

Do you know what memory management is and why ArrayBuffer and SharedArrayBuffer were introduced to JavaScript? This translated article from Lin Clark, enriched with illustrations, walks you through the concepts.

Think of a computer's memory as a collection of boxes, each with a numeric address. Boxes have uniform size (the word length, typically 32‑ or 64‑bit, shown here as 8‑bit for simplicity). Storing a number like 2 is simply converting it to binary; storing characters such as H requires an encoding like UTF‑8 and a decoder to retrieve it later.

Automatic Memory Management

When you write JavaScript, you generally do not deal with memory directly; the JavaScript engine abstracts it away and performs allocation, tracking, and garbage collection on your behalf.

For example, a piece of JS code (perhaps using React) creates a variable. The engine encodes the value into binary, finds a suitable memory region (allocation), and monitors whether the variable remains reachable. When it becomes unreachable, the engine frees the memory—a process known as garbage collection. This convenience comes with performance overhead that can affect predictability.

Manual Memory Management

Languages that require manual memory handling, such as C (now usable in the browser via WebAssembly), operate directly on memory without the abstraction layer provided by JavaScript. When compiling C or other languages to WebAssembly, the toolchain adds runtime code for encoding/decoding bytes, but it does not include a garbage collector.

Even in manual‑managed languages, the runtime often assists by maintaining a free‑list of available addresses. Developers allocate memory with malloc and release it with free , removing and returning addresses to the free‑list. Knowing when to call these functions is crucial; mistakes can cause bugs, security vulnerabilities, or memory exhaustion.

Modern languages favor automatic memory management to avoid such errors, accepting the trade‑off of additional overhead.

For a deeper dive, the original English article is linked at the end, and this piece is the first of a three‑part series on memory management.

JavaScriptMemory ManagementWebAssemblyGarbage CollectionArrayBuffermanual memory
Hujiang Technology
Written by

Hujiang Technology

We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.