# 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.

```javascript
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.

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascript-1.gitbook.io/javascript/call-stack-and-memory-heap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
