Stack vs Heap Memory Allocation
Stack Memory Allocation
Its a temporary memory allocation scheme where the data members are accessible only if the method that contained them is currently running
It allocates or de-allocates the memory automatically as soon as corresponding method completes its execution.
#Stack_memory allocation is considered safer as compared to #heap_memory allocation because the data stored can only be accessed by the owner thread.
memory allocation and de-allocation is faster than compared to head-memory
#Stack_memory has less storage space as compared to #heap_memory
Heap Memory Allocation
The name heap has nothing to do with the heap data structure It is called a heap because it is a pile of memory space available to programmers to allocate and de-allocate.
Every time when we made an object it always creates in Heap-space and the referencing information to these objects is always stored in Stack-memory
Heap memory allocation isnt as safe as #Stack_memory allocation because the data stored in this space is accessible or visible to all threads.
Heap memory is accessible or exists as long as the whole application runs
Last updated