Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Memory Management and DevTools  ›  Lesson

Memory Lifecycle

Memory Lifecycle 10 minEvery value goes through allocation (space set aside when it's created), use (the program reads and writes it), and release (every reference to it is dropped) — JavaScript handles allocation and reclamation automatically, but release only ever means removing references; the engine decides on its own when the memory is actually reclaimed.

Memory Lifecycle

In short: every value's life has three stages. Allocate: space is set aside the moment you create it (let cart = {...}). Use: the program reads and writes it while it's needed. Release: every reference to it is dropped, and it becomes eligible to be cleaned up. JavaScript handles all three automatically — you never call anything like malloc/free yourself. The one thing worth being precise about: "release" means dropping references, not the moment memory is actually reclaimed. The engine decides when that happens, on its own schedule, never something your code can observe or control directly.

A warehouse loading bay, three stages: 1. allocate — let cart = {...}, space set aside on the truck; 2. use — cart.total += 50, the program reads and writes to it; 3. release — cart = null, the last reference is dropped; a note that "release" means dropping every reference — the engine decides WHEN it actually reclaims that memory

Watching the three stages happen

The same three stages, in order, on one ordinary object:

javascript

Nothing here is special syntax — allocation is just { ... } being evaluated, use is just reading and writing properties, and release is just letting go of the variable that used to point at it. Once cart is null and nothing else references the original object, it's eligible to be cleaned up — but exactly when that happens is not something this code can show you.

Primitives copy, objects share a reference

Whether a value gets copied or shared affects what "the last reference" even means — a detail from the data-types lessons that matters again here:

javascript

count and sameCount are two completely independent values after this — changing one never affects the other, and each has its own separate "last reference" moment. Objects behave differently: assigning one variable to another copies the reference, not the object, so multiple variables can point at the exact same allocated memory, and it only becomes eligible for release once all of them let go.

Quick practice

quick drill

What does "allocate" mean in the memory lifecycle?

quick drill

After let cart = {...}; cart = null;, is the object's memory freed at that exact line?

quick drill

Why does let a = 5; let b = a; b = 10; leave a unchanged, while doing the same with two variables pointing at the same object would NOT leave the other one unchanged?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhat release actually means

After let cart = { items: [...] }; cart = null;, explain what has actually happened to the object the variable used to point to, and what has NOT happened (yet, or possibly ever, in a way your code can observe).