java.lang.StackOverflowError

In this article, we will discuss about the java.lang.StackOverflowError by looking different code examples and also how can we avoid them.

More precisely, StackOverflowError is an error which Java doesn’t allow to catch, for instance ,stack running out of space, as it’s one of the most common runtime errors one can encounter, because it’s raising condition is implemented directly or indirectly in almost every module.

The main cause of the java.lang.StackOverflowError is that we haven’t provided the proper terminating condition to our recursive function or template, which means it will turn into an equivalent of an infinite loop.


How JVM invokes a method

To understand about java.lang.StackOverflowError, we need to understand how JVM invokes method internally. When a method is invoked, a new stack frame is created on the Thread Stack Size or on the call stack.
Now, that respective stack frame holds arguments of the invoked method, mainly the local variables and the return address of the that method. This creation of stack frames will be an iterative process, and will be stopped only when the end of method invocations is found inside nested methods.

In amid of this whole process, if JVM is running out of space for the new stack frames to be create, it will throw a java.lang.StackOverflowError.

( This is the reason, why we call it as JVM error also. )


Reasons for java.lang.StackOverflowError

There can be multiples reasons for java.lang.StackOverflowError

Poor or No Termination Condition

This is most common situation termed as unterminated or infinite recursion.

Let’s see an example where the error will be thrown as a result of the given condition:

In the following example, we are trying to print natural numbers, without providing the proper condition.

< pre class= "java" name= "code" >
public class java2blog
{
static int i=0 ; // declaring static variable it cannot be from a static context

}


Output:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
..
..
Exception in thread "main" java.lang.StackOverflowError
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)
at java2blog.printNum (java2blog.java:8)

Solution

This can be easily done by changing the settings of our compiler.

< pre class= "java" name= "code" >
public class java2blog
{
static int i=0 ; // declaring static variable it cannot be from a static context

}

Output: 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Due to the Cyclic Relationships Between Classes

Cyclic Relationship : When two different classes instantiate each other inside their constructors causes this relationship.

This ends up with a StackOverflowError because the constructor of ClassA is instantiating ClassB, and the constructor of ClassB is again instantiating ClassA, and it occurs repeatedly until the overflow.
< pre class= "java" name= "code" >
public class A
{
public B type2 ;
public A() {
type2 = new B () ; // object of class B is created and hence constructor of // B is invoked
}

// at the time of object declaration (implicit)
}
}
class B
{
public A type1 ;
public B () {
type1= new A () ; // object of class A is created and hence constructor of // A is invoked
}
}

Output:

Exception in thread “main” java.lang.StackOverflowError
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)
at A.(A.java:5)
at B.(A.java:16)

Repetitive calls here shows the infinite recursion

Solution

Now, the error here is mainly due to the constructors calling each other repetitively, so we have to make sure that while implementing different classes, the above condition should not arise.
Since there is no significance of this cyclic constructors invocations so we can avoid them, as they are already invoked implicitly at the time of the creation of the objects of their respective classes.

That’s all about java.lang.StackOverflowError in java.




Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *