On this page

Memory Management: The Call Stack and Memory Heap

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

JavaScript is a high-level language that handles memory management automatically through a process called Garbage Collection. However, for an engineer to write performant, industrial-grade code, it is necessary to understand how the engine allocates physical hardware resources. Memory management in JavaScript is divided into two primary structures: the Call Stack and the Memory Heap.

The Call Stack: Static Memory Allocation​

The Call Stack is a high-speed memory structure that operates on the LIFO (Last In, First Out) principle. Its primary responsibility is to keep track of function execution and store primitive values.

The Memory Heap: Dynamic Memory Allocation​

Unlike the stack, the Memory Heap is a large, mostly unstructured region of memory. It is used for Dynamic Allocation, where the size of the data may change over time or is not known upfront.

The Garbage Collection Process​

JavaScript utilizes an automatic Garbage Collector (GC) to free up memory that is no longer being used. The goal of the GC is to identify "unreachable" memory—data that the program can no longer access.

The Mark-and-Sweep Algorithm​

Modern engines, such as V8, primarily use the Mark-and-Sweep algorithm. The process follows these stages:

  1. Marking: The GC starts at the "roots" (the global object and the current call stack) and traverses all references. Every object it can reach is "marked" as active.
  2. Sweeping: The GC then scans the Memory Heap. Any object that was not marked during the first phase is considered unreachable.
  3. Reclaiming: The memory occupied by these unmarked objects is released back to the system.

Common Memory Leaks​

Even with automatic garbage collection, developers can inadvertently create Memory Leaks by keeping references to data that is no longer needed, preventing the GC from "sweeping" it.

Global Variables​

Variables attached to the window or global object are never garbage collected as long as the application is running. Accidental globals (variables declared without let, const, or var) are a frequent source of leaks.

Forgotten Timers and Callbacks​

If a setInterval is started and never cleared using clearInterval, any variables referenced inside that interval's closure will remain in memory forever, even if the rest of the application no longer needs them.

Detached DOM Nodes​

If you store a reference to a DOM element in a JavaScript variable and then remove that element from the page using remove(), the memory for that element cannot be reclaimed because the JavaScript variable still "points" to it.


// A potential leak: storing a reference to a deleted element

let btn = document.getElementById("submit-btn");

document.body.removeChild(btn);



// 'btn' still exists in the Memory Heap because the variable holds a reference.

// To fix this:

btn = null;

Hardware Efficiency Standards​

To ensure your applications remain fast and responsive, observe these memory-safety rules:

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Closures

Next

Currying & Composition