Memory Leak

Memory in JS is divided into two main areas, the Heap and the Stack. The Stack is where primitives and references to objects are stored, while The Heap is where objects and arrays are kept (technically, strings too, but they are treated like primitives for convenience).

JavaScript has automatic garbage collection, which is great, but it doesn’t mean that memory can't be mismanaged. Press → to see.

The garbage collector typically uses a mark-and-sweep algorithm. The collector starts by identifying root objects, such as global variables. From there, it traverses through all connected references, tagging each encountered object as active or in use. Finally, it sweeps through the entire Heap, freeing unmarked memory for reuse.