JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between Heap and Stack Memory in Java JVM ?

Difference between Heap and Stack Memory in Java JVM



  1. Heap memory is shared by all threads of Java application but Stack memory is local to each thread. 
  2. Objects are created in heap memory but method frames are stored in Stack memory, and size of heap space is much bigger than the small size of Stack in Java



Stack vs Heap in Java

As I told, both Stack and Heap space are part of JVM but they are used for different purpose, let's see some more points to understand the difference between stack and heap memory better.


1) Size
One of the significant difference between Stack and heap comes from their size. Heap space in Java is much bigger than the Stack memory. This is partly due to the fact that whenever a new thread is created in JVM, a separate stack memory is allocated to them.






2) Resizing
JVM allows you to resize both heap and stack in Java, though you need to use different JVM flags for that. You can use -Xms and -Xmx to specify the starting and maximum heap memory in Java. Similarly, you can use the -Xss to specify stack size of individual threads in JVM. See Java Performance Companion by Charlie Hunt to learn more about JVM tuning in Java.



3) Usage
Another significant difference between heap and stack memory comes from their usage perspective. Heap memory is used to store objects in Java. No matter where you create object e.g. inside a method, a class or a code block, they are always created in heap space and memory is allocated from the heap.

One little exception of that is String literals which live in String pool, which was not part of heap untile Java 7. Earlier String pool was created on meta space, which was separate memory are in JVM used to store class metadata, but from JDK 7 onwards String pool is merged into heap space.

On the other hand, Stack memory is used to store local variables e.g. primitive int and boolean variables, method frames and call stack.



4) Visibility
One more difference between heap and stack memory comes from visibility and sharing perspective. Heap memory is shared by all threads hence it is also known as the main memory but stack memory is local to threads and local variable created there was not visible to others. Threads can also cache values into Stack memory. See Java Performance by Binu John to learn more about heap and stack memory in Java.



Difference between Heap and Stack Memory in Java JVM


5) Order
Heap is a large memory area where objects can be created and stored in any order but Stack memory is structured as Stack data structure i.e. LIFO where method calls are stored as last in first out order. This is why you can use recursion in Java.


6) Error
You get different errors when heap or stack memory gets filled. For example, a faulty recursive algorithm can quickly make Stack memory filled up with recursive method calls in that case you will see java.lang.StackOverFlowError, but when there is no more space left in heap to allocate a new object than you will see the OutOfMemoryError in java e.g. java.lang.OutOfMemoryError: Java Heap Space. This is another useful difference to know between Stack and Heap in Java from debugging and troubleshooting perspective.


.