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

 


Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com
Page Views : 309    Published Date :Nov 03,2019   Author :rajneesh
Please Share this page

Popular Articles

Loading...
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 moreGot it!