Articles

Difference between Heap Memory vs Stack Memory in java

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? 

  1. Heap memory
  2. Stack memory
  3. Method area
  4. Classloader
  5. Program counter registers
  6. 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

 


Java Interview Questions

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  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!