In this tutorial, we will discuss how to solve the java.lang.stackoverflowerror – StackOverflow Error in Java. This error is thrown to indicate that the application’s stack was exhausted, due to deep recursion.
StackOverflowError extends the
VirtualMachineError class, which indicates that the JVM is broken, or it has run out of resources and cannot operate. Furthermore, the the
VirtualMachineError extends the
Error class, which is used to indicate those serious problems that an application should not catch. A method may not declare such errors in its
throw clause, because these errors are abnormal conditions that shall never occur.
StackOverflowError exists since the 1.0 version of Java.
You can also check this tutorial in the following video:
1. The Structure of StackOverflowError
Creates an instance of the
StackOverflowError class, setting
null as its message.
Creates an instance of the
StackOverflowError class, using the specified string as message. The string argument indicates the name of the class that threw the error.
2. The StackOverflowError in Java
When a function call is invoked by a Java application, a stack frame is allocated on the call stack. The stack frame contains the parameters of the invoked method, its local parameters, and the return address of the method. The return address denotes the execution point from which, the program execution shall continue after the invoked method returns. If there is no space for a new stack frame then, the
StackOverflowError is thrown by the Java Virtual Machine (JVM).
The most common case that can possibly exhaust a Java application’s stack is recursion. In recursion, a method invokes itself during its execution. Recursion is considered as a powerful general-purpose programming technique, but must be used with caution, in order for the
StackOverflowError to be avoided.
An example that throws a
StackOverflowError is shown below:
In this example, we define a recursive method, called
recursivePrint that prints an integer and then, calls itself, with the next successive integer as an argument. The recursion ends once we invoke the method, passing
0 as a parameter. However, in our example, we start printing numbers from
1 and thus, the recursion will never terminate.
A sample execution, using the
-Xss1M flag that specifies the size of the thread stack to equal to 1MB, is shown below:
Depending on the JVM’s initial configuration, the results may differ, but eventually the
StackOverflowError shall be thrown. This example is a very good example of how recursion can cause problems, if not implemented with caution.
3. More about the java.lang.stackoverflowerror
The following example demonstrates the risk of having cyclic relationships between classes:
In this example, we defined two classes,
B. The class
A contains one instance of the
B class, while, the
B class contains one instance of the
A class. Thus, we have a circular dependency between these two classes. Furthermore, each
toString method, invokes the corresponding
toString method of the other class, and so on, which results in a
A sample execution is shown below:
4. How to deal with the java.lang.stackoverflowerror
- The simplest solution is to carefully inspect the stack trace and detect the repeating pattern of line numbers. These line numbers indicate the code being recursively called. Once you detect these lines, you must carefully inspect your code and understand why the recursion never terminates.
- If you have verified that the recursion is implemented correctly, you can increase the stack’s size, in order to allow a larger number of invocations. Depending on the Java Virtual Machine (JVM) installed, the default thread stack size may equal to either
1MB. You can increase the thread stack size using the
-Xssflag. This flag can be specified either via the project’s configuration, or via the command line. The format of the
5. Additional knowledge
6. Download the Eclipse Project
This was a tutorial about the
StackOverflowError in Java.
You can download the full source code of this example here: java.lang.StackOverflowError – How to solve StackOverflowError
Last updated on Apr. 06th, 2020