Stack vs Heap memory
Amul Gaurav
Posted on July 27, 2024
This concept isn't different to any other programming language, so any literature about heap and stack applies the same to your preferred programming language. So, you might not want to skip it thinking this probably doesn't apply to the language in which I code. Let's dive right in!
Stack Memory
- Location: Stack memory resides in RAM.
- Purpose: It is used for static memory allocation, which includes storing function parameters, return addresses, local variables, and control flow data.
- Structure: The stack operates in a Last In, First Out (LIFO) manner.
- Size: The size of the stack is generally determined at the start of a program and is limited. The exact size can vary depending on the operating system and the architecture, but it is typically much smaller than the heap. Practically, stack size might be a few megabytes.
- Management: Stack memory is managed automatically by the CPU, with memory being allocated and deallocated as functions are called and return.
Heap Memory
- Location: Heap memory also resides in RAM.
- Purpose: It is used for dynamic memory allocation, where memory blocks are allocated and freed as needed by the program.
- Structure: The heap does not follow a strict order and can grow and shrink dynamically.
- Size: The heap size is generally much larger than the stack size. It can be many gigabytes, depending on the system's available memory and configuration.
- Management: Heap memory is managed manually by the programmer using allocation (malloc, new) and deallocation (free, delete) functions. Some modern languages (like Java & Python) have garbage collectors to manage heap memory automatically.
Now let's discuss when you write code how your variables get stored.
1. 1st Scenario:
If you want your variable to be available in the function scope & gets cleared from the memory as soon as the function returns, you will declare it like you normally do & the variable will get stored in Stack memory. A stack frame is added when a function is called and removed when the function returns. Here's an example how you can do that in JAVA:
static void sample() {
int a = 5;
}
In the above code block, the variable a
is defined in the sample
function scope & will only be available in that scope when the sample
function gets called. It will be cleared from the stack as soon as the sample
function call returns.
2. 2nd Scenario:
If you don't want the behavior like in Scenario 1. There are 2 options to avoid that:
- Using Stack: as soon as the function call finishes and function gets cleared from the stack, the variable stored gets cleared from its current function call stack & gets copied to its parent function call stack.
- Using Heap: The most preferred & efficient way to achieve this is to create a pointer. A pointer is a reference variable which is stored in stack memory points to a block of heap memory. Such an allocation is independent of function calls. This means the heap can become fragmented and the OS needs to keep track of which memory is used and which is free. This is quite a lot of work compared to the stack where only one pointer is used to point to the currently active frame. The data stored in heap memory can be pointed or referenced by more than one reference variable.
Student student = new Student();
// student -> object of class Student
// student -> reference variable
// new Student() -> new keyword dynamically allocates memory for student object of class Student() in heap memory
When there is no or zero reference variable pointing to the memory allocated in heap, it's get garbage collected by the Garbage Collector, in case of languages that have garbage collector like JAVA.
In languages like C or C++ where there is no concept of Garbage Collectors, the memory allocated in heap must be handled manually by the programmer using (malloc, new) functions for allocation and (free, delete) functions for deallocation.
Stack is simple, fast and limited. Heap is flexible and almost unlimited but slow to allocate and a lot of work for the OS to manage.
Bonus: When primitives are declared as instance variables within a class, they are part of the object's memory and are allocated on the heap.
References
- Learning Go: A Discussion of the Language with the Author, Jon Bodner | 028
- Trying to understand Stack & Heap
That concludes this article. If you need further clarification, please comment below, and I'll do my best to assist you.
Posted on July 27, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.