Difference between Heap and Stack Memory in
Java JVM
One
of the many traits of a good programmer is how well he understands the
fundamental and if you want to check the fundamentals of Java programmer than
asking the difference between heap and stack memory is a good choice. Even
though both are part of JVM and both consumers memory allocated to the Java
process, there are many differences between them e.g. Heap
memory is shared by all threads of Java application but Stack memory is
local to each thread. 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. Even if you know this much information about heap
and stack in Java, you are one of the better candidates, but let's see some
more details to impress the interviewer.
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.
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.
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.
That's all about the difference between Stack and Heap memory in Java application. It's extremely important for any Java developer, fresher or experienced to know about these fundamentals. If you don't know about the heap, it would be very difficult to survive or clear any Java programming interviews. Remember, stack memory is used to store local variables and methods calls while heap memory is used to store objects, also heap memory is much larger than stack memory but access to Stack is faster than the heap. See Java Performance The Definitive Guide By Scott Oaks to learn more about heap and stack memory in Java.
No comments:
Post a Comment