Based on my previous post related to Java Memory Allocation – Heap and Stack let’s see the difference.

Heap Space Stack Memory
Heap memory is used by all the parts of the application. Stack memory is used only by one thread of execution.
Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.
Objects stored in the heap are globally accessible. Stack memory can’t be accessed by other threads.
We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory. We can use -Xss to define the stack memory size
When Heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error When stack memory is full, Java runtime throws java.lang.StackOverFlowError
Categories: JAVA


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *