In this article, let’s have a look at STACK VS HEAP MEMORY.
we are going to learn about the stack, heap memory, and the differences between them in a very detailed way
Stack memory is a type of memory that is used by a computer program to store temporary data. It is called “stack” memory because it is organized in a last-in-first-out (LIFO) structure, like a stack of plates.
When a program is executed, the system allocates a certain amount of stack memory to it. This memory is used to store the local variables, function arguments, and other temporary data that are needed while the program is running. When a function is called, its local variables and arguments are pushed onto the stack. When the function returns, these values are popped off the stack.
Stack memory is different from heap memory, which is used to store dynamically-allocated objects that have longer lifetimes than the local variables and arguments stored on the stack. Because stack memory is organized in a LIFO structure, it is generally faster to access than heap memory. However, it is also limited in size, so programs that use a lot of stack memory may run into memory errors if they exceed the allocated stack size.
Heap memory is a type of memory that is used by a computer program to store dynamically-allocated objects. It is called “heap” memory because it is organized as a heap data structure, which allows objects to be efficiently allocated and deallocated at runtime.
When a program is executed, the system allocates a certain amount of heap memory to it. This memory is used to store objects that are created dynamically at runtime, such as objects that are allocated using the “new” keyword in C++ or the “malloc” function in C. These objects have lifetimes that are determined by the program, rather than by the stack memory allocation of the function in which they are created.
Heap memory is different from stack memory, which is used to store local variables, function arguments, and other temporary data. Because heap memory is organized as a heap data structure, it is generally slower to access than stack memory. However, it is not limited in size like stack memory, so programs can allocate as much heap memory as needed, as long as there is sufficient physical memory available on the system.
The Heap-memory allocation is further divided into three categories:-
Young Generation – It’s the portion of the memory where all the new data(objects) are made to allocate the space and whenever this memory is completely filled then the rest of the data is stored in Garbage collection.
Old or Tenured Generation – This is the part of Heap-memory that contains the older data objects that are not in frequent use or not in use at all are placed.
Permanent Generation – This is the portion of Heap-memory that contains the JVM’s metadata for the runtime classes and application methods.
These were the major differences between stack and heap memory
|Memory is allocated in a contiguous block.||Memory is allocated in any random order.|
|It can access high speed.||It is slower than the stack.|
|It accesses only local variables.||It accesses global variables.|
|Variables cannot be resized.||Variables can be resized.|
|The cost is less.||The cost is more.|
|The memory is allocated d contiguous block.||The memory is allocated randomly.|
|There is a shortage of memory in the n stack.||There is memory fragmentation.|
|It is a fixed size.||It can be resized.|
|There is a limit on the stack size.||It does not have any specific limit on memory size.|
|The stack is excellent.||The heap is adequate.|
|The stack is linear.||The heap is hierarchical.|
|Automatic by compiler instructions||Manual by the programmer.|
BENEFITS OF STACK:
The implementation is very easy.
Stack frame access is easier than the heap frame.
The allocation and de-allocation are automatically done by the compiler.
The stack is safe and secure.
NON-BENEFITS OF STACK:
The stack is very limited memory.
The stack is not flexible.
The memory size allotted cannot be changed.
Random access is not possible.
BENEFITS OF HEAP:
Heap is flexible.
Memory space is available to programmers to allocate and de-allocate.
It allows you to access variables globally.
Heap doesn’t have any limit on memory size.
NON-BENEFITS OF HEAP:
Handling the Heap frame is costlier than handling the stack frame.
The size of the Heap-memory is quite larger.
A memory leak may happen if not handled the memory with care.
The heap is time taking than a stack.
It is user dependent. If the programmer uses the large arrays then they may use the heaps if not choosing the stack would be a better choice.
That’s it for this article. In the next article, let’s have a look at Physical vs Logical Data structures.