Difference between Heap Memory vs Stack Memory in java
In interviews, difference between heap memory and stack memory in java is a commonly asked question.
Probably you may also face this question in your next interview.
Moreover stack and heap are two commonly used terms in perspective of java.
Different kinds of memory allocated in java programming?
-
Heap memory
-
Stack memory
-
Method area
-
Classloader
-
Program counter registers
-
Native method stack
Difference between stack and heap memory
Given below is the major differences between heap memory and stack memory:
Heap memory
|
Stack memory
|
In java, heap memory generally stores objects (eg, String objects are stored in heap memory)
|
In java, stack memory generally stores local variables. It can also be used to store function calls.
|
If heap is running out of space, JVM throws java.lang.OutOfMemoryError
|
If Stack is running out of space, JVM throws java.lang.StackOverFlowError
|
Mostly size of heap is much larger than that of stack memory.
|
Mostly size of stack is much smaller in comparison to heap memory
|
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Sonu Pandit
Technology geek, loves to write and share knowledge with the world. Having 10+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :
Published Date :
Jun 28,2020