StackOverflowError in Java with Examples


When the stack ran out of space then StackOverflowError occurs. It is a runtime error indicating that JVM has run out of resources. The main reasons for this kind of error may be: Circular references, infinite Recursion or heavy application that utilizes more memory.

In this article, we will discuss the above listed reasons for StackOverflowError in the simplest way possible.

StackOverflowError in Java

Memory Mnagement in java

Before moving to the StackOverflowError it is important to know about how memory space managed by java for a particular program or application.

Every interface, class, object, variable and method of a running program is stored in distinct reasons of computer memory. The reference variables, names of methods and classes are stored in the stack and their values are stored in the heap.

Whenever a process starts it automatically gets defined with a fixed stack size. During each method call, a call frame gets created on the call stack and it continues until method invocation ends. When there is no space left for a new stack frame in the stack space of computer memory then we encounter StackOverflowError.

Syntax

import java.lang.StackOverflowError;

This class is available in ‘java.lang’ package. We don’t necessarily need to import this package. Usually, the classes of this package are automatically available without importing them in our program. However, there are some exceptions too.

Example 1

The following example illustrates the StackOverflowError.

import java.lang.StackOverflowError;
public class Overflw {
   public static void methodA(int n1) {
      n1++;
      methodB(n1);
   }
   public static void methodB(int n1) {
      n1++;
      methodA(n1);
   }
   public static void main(String []args) {
      int n1 = 0;
      methodA(n1);
   }
}

Output

Exception in thread "main" java.lang.StackOverflowError
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)
	at Overflw.methodA(Overflw.java:5)
	at Overflw.methodB(Overflw.java:9)

As you can see the output of example 1 code we are getting StackOverflowError. What we have done in the above code is that we have created two parameterized user defined methods named ‘methodA’ and ‘methodB’. In the main method, we have declared and initialized an integer variable ‘n1’ to 0 and called the ‘methodA’ along with an argument ‘n1’. Now, the ‘methodA’ called the ‘methodB’ with the increment value of ‘n1’. Again, the ‘methodB’ called the ‘methodA’ and this process repeat itself many times. Hence at some point, the stack size created for this program gets exhausted resulting in the following error.

The following measures we can take to handle StackOverflowError −

  • Carefully inspect those lines that repeat themselves and try to find the reason why they didn’t terminate.

  • The default stack size may be either 512KB Or 1MB depending on the installed Java Virtual Machine but we can increase the size of stack manually to avoid this error. However, this technique is rarely used.

Example 2

Now, with the help of this example we will try to find the solution for StackOverflowError occured in the earlier example.

public class Overflw {
   public static void methodA(int n1) {
      n1++;
      methodB(n1);
   }
   public static void methodB(int n1) {
      n1++;
      int n2 = 5;
      int mult = n1 * n2;
      System.out.println("Value of n1 and n2 multiplication is: " + mult);
   }
   public static void main(String []args) {
      int n1 = 0;
      methodA(n1);
   }
}

Output

Value of n1 and n2 multiplication is: 10

In the example 1 the problem with the program was that the lines 6 and 10 didn’t get terminated. But in the above example, we have given a statement that terminates the program and prints the value stored in ‘mult’.

Conclusion

The StackOverflowError occurs at runtime, not during compilation that’s why it is important to handle it properly otherwise it may cause trouble for us. The most effective way to tackle this is by carefully inspecting the lines where error occurs.

Updated on: 05-May-2023

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements