June 28, 2022

StackOverflowError Vs OutOfMemoryError in Java

While working on and running a Java application you may come across any of these two errors related to memory, StackOverflowError and OutOfMemoryError. In this post we’ll see the differences between StackOverflowError and OutOfMemoryError in Java.

StackOverflowError in Java

A JVM stack is created for each JVM thread. Whenever a method is invoked a new frame is created and pushed into the JVM stack for the thread. Each frame stores data related to the method like local variables, operand stack and a reference to the run-time constant pool of the class of the current method. Once the method execution is completed stack frame (for that method) is popped out of the stack.

If execution of any method requires a larger stack than is permitted, the Java Virtual Machine throws a StackOverflowError. You may see StackOverflowError when you have a recursive method with no terminating condition.

For example a Java program to recursively print even numbers without any terminating condition.

public class StatckOverFlowErrorExample {
  public static void main(String[] args) {
    printEven(1);
  }
	
  private static int printEven(int i) {
    if(i % 2 != 0) {
      i++;
    }
    System.out.println(i);
    return i + printEven(i + 2);
  }
}
Output
20808
20810
20812
Exception in thread "main" java.lang.StackOverflowError
	at java.base/java.io.PrintStream.write(PrintStream.java:605)
	at java.base/java.io.PrintStream.print(PrintStream.java:676)
	at java.base/java.io.PrintStream.println(PrintStream.java:812)
	at com.knpcode.proj.Programs.StatckOverFlowErrorExample.printEven(StatckOverFlowErrorExample.java:14)
	at com.knpcode.proj.Programs.StatckOverFlowErrorExample.printEven(StatckOverFlowErrorExample.java:15) 

OutOfMemoryError in Java

Whenever you create a new object memory for that object is allocated on the heap. Instance variables and arrays are also stored on the heap.

Once the object stored on the heap is not having any reference, memory for that object is reclaimed by garbage collector. If there are references to the object then GC can’t remove those objects, if you have large number of such referenced objects and JVM tries to allocate heap memory for new object, JVM throws java.lang.OutOfMemoryError because there is no sufficient heap memory left.

Trying to allocate an array that is larger than the heap size also results in OutOfMemoryError.

public static void main(String[] args) {
	Integer[] array = new Integer[1000*1000*1000];
}
Output
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

StackOverflowError Vs OutOfMemoryError in Java

StackOverflowError OutOfMemoryError
StackOverflowError is thrown when the stack is full. OutOfMemoryError is thrown when the heap space is full.
Stack is used to store method related data when any method is invoked. So, StackOverflowError is thrown when there is no space left for storing method data. Heap memory is used to store objects, instance variables and arrays. So, OutOfMemoryError is thrown when no space left for creating new objects, arrays.
Recursive methods with out terminating condition causes StackOverflowError. Lots of objects with live references so GC can’t deallocate those objects results in OutOfMemoryError.
To avoid StackOverflowError ensure that methods are executing as per logic and terminating so that the stack frames for the executed methods can be taken out of the stack. To avoid OutOfMemoryError ensure objects which are no longer required are not reference from any where and can be garbage collected. Also ensure not to create very large objects or arrays.

That's all for the topic StackOverflowError Vs OutOfMemoryError in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment