Call Stack & Memory heap

Call Stack:

  • Call Stack is the place where the code execution has been tracked.

  • Every data in the call stack will be pointed to the memory heap.

  • Follows Last In First Out (LIFO).

Memory Heap:

  • Memory heap is the place where the memory is allocated for the variables and functions etc.

Stack Overflow:

  • When the function runs inside and inside, the call stack will be filled and overflows.

  • When the stack overflows, Maximum call stack size exceeded error will be thrown.

  • The below function runs inside and inside and the stack will be overflowed.

Ex: 
function inception() {
    inception();
}
 inception();

Garbage Collection:

  • JavaScript automatically cleans out the memory allocated to the data after its execution. For example when the memory allocated for the variable created inside the function, it will be cleared after the function execution automatically.

  • It will manages the memory leaks.

  • It will automatically controls the memory heap.

  • It follows Mark and Sweep algorithm to handle Garbage collection.

Memory Leaks:

  • Memory leaks occurred when the data is overloaded (Infinite data into an array), it breaks the browser.

Ex:
let array = [];
for(let i=1; i>0; i++) {
    array.push(i);
}

Last updated