Sub topic 1
How JavaScript Allocates and Releases Memory
Every program uses memory in the same three steps. It allocates the memory it needs, it uses that memory by reading and writing it, and it releases the memory when it is done. Languages such as C make you do the first and last steps yourself with malloc and free. JavaScript does both of them for you. This chapter shows where each step happens in ordinary code, and why the release step is the one that leads to bugs.
Prerequisites
This chapter assumes you can:
- Declare variables and create objects, arrays, and functions in JavaScript.
- Explain in rough terms what scope and closures are.
- Run code in a browser console or with Node.js.
You do not need to know anything about engine internals yet.
The Three Phases
| Phase | Who does it | What it looks like in code |
|---|---|---|
| Allocate | The engine | Literals, `new`, function definitions, many built-in methods |
| Use | Your code | Reading properties, assigning values, calling functions |
| Release | The garbage collector | Nothing. There is no `free` call |
Allocation Happens Implicitly
You never ask for bytes directly. The engine reserves memory each time your code creates a value that needs it:
const count = 42; // a number
const greeting = "hello"; // a string
const point = { x: 1, y: 2 }; // an object and its properties
const list = [1, 2, 3]; // an array
const now = new Date(); // an object created by a constructor
function add(a, b) { return a + b; } // a function is an object too
// Many methods return new values and allocate as a result
const upper = greeting.toUpperCase(); // a new string; greeting is unchanged
const doubled = list.map((n) => n * 2); // a new array
const merged = { ...point, z: 3 }; // a new object
console.log(count, upper, doubled, merged, now instanceof Date, add(2, 3));The last group matters most. Calls like map, slice, concat, template literals, and object spread all create new values. Code that looks like it only reads data may still be allocating on every call.
Use Is the Part You Control
Using memory means reading and writing values: point.x = 10, list.push(4), passing point into a function. Some writes also allocate, because a push can make the array grow its backing storage. This phase is where your design choices decide how long each value stays in use.
Release Is Automatic, and Indirect
JavaScript has no operator that frees an object. Setting a variable to null does not destroy anything. It only removes one way of reaching the object:
let config = { retries: 3 };
const cache = [config];
config = null; // the object still exists, cache[0] holds it
console.log(cache[0].retries); // 3The garbage collector frees memory once it can show that no part of the running program can reach the value any more. Later chapters cover how it proves that. The practical point for now is that release depends on references you may have forgotten about. A value stays in memory while anything still points to it, whether that is a cache, a timer, an event listener, or a closure.
Summary
- The engine allocates memory implicitly through literals, constructors, functions, and methods that return new values.
- Your code uses that memory, and many common operations allocate along the way.
- The garbage collector releases memory once a value is unreachable. You influence when that happens by dropping references. You cannot free a value directly.
Exercise
In the code let a = { n: 1 }; let b = a; a = null;, is the object { n: 1 } eligible for collection after the third statement? Explain why.
Show answer
No. Setting a to null removes only one reference. The variable b still points to the same object, so the program can still reach it and the collector must keep it. It becomes eligible only after b also stops referring to it.
Exercise
Which of these lines allocate a new object or string: arr.length, arr.slice(), ` id-${n} , obj.x`?
Show answer
arr.slice() allocates a new array, and the template literal ` id-${n} allocates a new string. arr.length and obj.x` only read existing values and do not allocate.