Heap Memory vs Stack Memory

Heap Memory vs Stack Memory

In interviews, difference between heap memory and stack memory is a common 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. Be prepared on below questions:


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
Page Views : 259    Published Date :Jun 29,2019   Author :rajneesh
Please Share this page

Popular 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 moreGot it!