Stack and heap are the memory blocks that follow the allocation techniques. The memory in various programming languages like C, C++, Java, can be allocated using stacks and heaps. Let us understand in detail how it is done. Firstly, we will look at the key differences between stack allocation and heap allocation.
Difference between Stack Allocation and Heap Allocation in tabular form
|Stack Allocation||Heap Allocation|
|In stack allocation, memory is allocated in the contiguous order||In heap allocation, memory is allocated in the random order|
|In stack allocation, the allocation and deallocation process is carried out automatically by the compiler instruction||In heap allocation, the allocation and deallocation process is carried out manually by the programmer itself|
|There is no requirement of deallocating variables||There is a requirement for deallocation|
|It is costlier than the heap||It is cheaper than the stack|
|Stack allocation has high access speed||Heap allocation has a low access speed|
|In this process, variables cannot be resized||In this process, variables can be resized|
|Implementation of stack consists of three types, using an array, dynamic memory, and linked list||Implementation of heaps can be done by using arrays and linked lists|
|The main issue in stack allocation is about the shortage of memory||The main issue in heap allocation is memory fragmentation|
|In stack allocation, the size is fixed||In heap allocation, the size can be modified as per requirement|
What is meant by stack allocation?
Stack is an important area of computer’s memory. It stores temporary variables that are created by the function. Stack is temporary storage that erases the data when the processor completes its task.
Stack allocation follows a particular structure named LIFO (Last In First Out) which means the last entry in the stack can be removed or accessible at any time. As said above it assigns the memory to the processor or CPU with the help of push and pop operation. The memory contains blocks and every block has a fixed size that can not be expanded or reduced in size.
Stack is defined by contiguous memory. It possesses a pointer that points to the first entry of the stack named as stack base and a pointer that points to the last or recent entry of the stack called stack top. Stack also supports function calls and whenever a function is called, the memory size that is to be allocated is already known to the compiler and the variables allocate the memory on the stack. Once the function call is over the memory is deallocated.
This might seem to be a bit complicated but the user doesn’t have to worry about it as the CPU takes the charge of the allocation and the deallocation of the stack. Also, we don’t have to worry about the memory which is consumed as the memory finishes its execution the data which was stored in the memory vanishes at once and the stack is again ready for the next execution. That means any value or data stored in the stack memory is valid or accessible as far as the execution is running.
Stack implementation is a bit complicated as compared to the implementation of the heap. Stack does not easily get corrupt and is less costly with fast access speed.
What is meant by heap allocation?
Heap is a memory that is used by computing languages to store global variables. Heap supports dynamic memory allocation. Unlike a stack, the heap is not automatically managed for you. Its working needs manual working from the programmer.
Heap allocation does not follow any specific approach, random assignment and reassignment of the memory are allowed by the heap. During allocation, the memory is executed according to the instruction written by the programmers. Here, the heap has nothing to do with the heap data structures. It is called heap because it is the pile of memory space provided to the user or programmer to allocate and deallocate.
We can say that heap allocation is not as safe and easy as stack allocation. This is because the value stored in heap allocation is visible to all the threads for CPU utilization. If the user or programmer is inefficient to manage this allocation properly then there are chances of memory leaks in the program.
There are three categories in the heap allocation.
• Young Generation: As per the name, here all the new and fresh data is stored and once this memory is filled then the remaining data goes into garbage collection.
• Old Generation: This part of heap memory stores the data which is not used frequently or the data which is completely unused.
• Permanent Generation: This part of heap memory contains the metadata of Java Virtual Machine for application methods.
Heap-memory size is much larger as compared to the stack-memory size. This is because heap memory is accessible as long as the whole program runs, not as stack memory.
We have seen the difference between stack allocation and heap allocation in this article.
SVKM’s Institute of Technology, Dhule