JavaScript Memory Management Interview Questions and Answers
Understand stack and heap storage, garbage collection, and common sources of memory retention.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- Does assigning null immediately free an object?
- What is garbage collection in JavaScript?
- What is a memory leak in a garbage-collected language?
- What is the heap used for in JavaScript?
- What does reachability mean for garbage collection?
- How can closures contribute to retained memory?
- How can event listeners retain objects?
Theoretical JavaScript Interview Questions
Garbage collection is JavaScript's way of automatically freeing up memory you're no longer using. You create objects and variables, and when nothing references them anymore, the garbage collector cleans them up. The main algorithm is "mark-and-sweep" - it marks all reachable objects starting from the root, then sweeps away everything unmarked. You don't manage memory manually, but be careful with closures, global variables, and forgotten event listeners - they can keep references alive and cause memory leaks.
JavaScript uses automatic garbage collection to free memory that is no longer reachable by your program. In simple terms, if nothing in your code can access a value anymore, the engine can remove it from memory. Modern JavaScript engines mainly use a mark-and-sweep style approach: reachable values stay, unreachable values are cleaned up.
Memory leaks happen when your code keeps references to data that is no longer needed, so the garbage collector cannot remove it. Common causes include forgotten timers, event listeners that are never removed, large objects captured by closures, and accidentally storing too much data in global variables or long-lived caches. Cleaning up references when you are done helps avoid these problems.
Garbage collection reclaims memory used by values that are no longer reachable. Developers do not manage it directly, but can cause leaks by retaining unnecessary references.
A value is reachable if the program can get to it through references from roots such as active contexts and globals. Unreachable values can generally be reclaimed, at runtime-chosen times.
No. The object remains alive if another reachable reference points to it, and garbage collection timing is nondeterministic.
A leak occurs when a program retains references to data it no longer needs, preventing collection. Memory use can grow despite automatic memory management.
The heap broadly refers to memory used for dynamically allocated objects and related data. The runtime manages allocation and collection rather than exposing manual heap control.
A closure keeps needed outer bindings reachable, and those bindings may reference larger structures. Release the closure or unnecessary references when that state is no longer needed.
A removed DOM node remains in memory if JavaScript still references it, perhaps together with listeners or other objects. Remove unneeded references so the object graph can become unreachable.
A listener can close over objects and remain registered on a long-lived target, keeping those objects reachable. Remove listeners when their lifecycle ends or use suitable AbortSignal options.
WeakMap stores object-keyed associations without making key reachability depend on the map, so metadata alone does not keep an otherwise unreachable key alive. It is not enumerable.
Compare snapshots at comparable points in a repeatable workflow and inspect objects that persist or grow unexpectedly. Retainer paths show references keeping objects alive.
A WeakRef target may be collected at any time, so deref() may stop returning it. Do not use WeakRef for essential state or deterministic resource cleanup.
Finalization callbacks may run much later or not before exit, and scheduling is nondeterministic. Use explicit cleanup for resources that require reliable release.