JavaScript · Memory Management · Junior · Concept
How does memory allocation and garbage collection work at a high level in JavaScript?
Short Interview Answer
JS allocates objects on the heap; GC reclaims values that are no longer reachable from roots (stack, globals, registered callbacks).
Detailed Explanation
Primitives may be handled specially, but objects/arrays/functions/closures live on the heap. Reachability from roots determines liveness — not whether you still 'think' you need the data. Engines use generational GC and techniques like mark-sweep/mark-compact and scavengers for young objects. Developers don't free memory manually, but they control reachability (nulling refs, removing listeners, closing over less). Understanding reachability is the practical skill.
Interview Tip
Define reachability — the key mental model.
Common Mistake
Thinking `delete obj.prop` guarantees immediate memory release of large graphs.