What Causes a Java.Lang.Stackoverflowerror

What is a StackOverflowError?

Parameters and local variables are allocated on the stack (with reference types, the object lives on the heap and a variable in the stack references that object on the heap). The stack typically lives at the upper end of your address space and as it is used up it heads towards the bottom of the address space (i.e. towards zero).

Your process also has a heap, which lives at the bottom end of your process. As you allocate memory, this heap can grow towards the upper end of your address space. As you can see, there is a potential for the heap to "collide" with the stack (a bit like tectonic plates!!!).

The common cause for a stack overflow is a bad recursive call. Typically, this is caused when your recursive functions doesn't have the correct termination condition, so it ends up calling itself forever. Or when the termination condition is fine, it can be caused by requiring too many recursive calls before fulfilling it.

However, with GUI programming, it's possible to generate indirect recursion. For example, your app may be handling paint messages, and, whilst processing them, it may call a function that causes the system to send another paint message. Here you've not explicitly called yourself, but the OS/VM has done it for you.

To deal with them, you'll need to examine your code. If you've got functions that call themselves then check that you've got a terminating condition. If you have, then check that when calling the function you have at least modified one of the arguments, otherwise there'll be no visible change for the recursively called function and the terminating condition is useless. Also mind that your stack space can run out of memory before reaching a valid terminating condition, thus make sure your method can handle input values requiring more recursive calls.

If you've got no obvious recursive functions then check to see if you're calling any library functions that indirectly will cause your function to be called (like the implicit case above).

What actually causes a Stack Overflow error?

It seems you're thinking that a stackoverflow error is like a buffer overflow exception in native programs, when there is a risk of writing into memory that had not been allocated for the buffer, and thus to corrupt some other memory locations. It's not the case at all.

JVM has a given memory allocated for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if you were trying to write at index N of an array of length N. No memory corruption can happen. The stack can not write into the heap.

A StackOverflowError is to the stack what an OutOfMemoryError is to the heap: it simply signals that there is no more memory available.

Description from Virtual Machine Errors (§6.3)

StackOverflowError: The Java Virtual Machine implementation has run out of stack space for a thread, typically because the thread is doing an unbounded number of recursive invocations as a result of a fault in the executing program.

Why am I getting a StackOverflowError exception in my constructor

Your main method creates a Cloning instance (Cloning c=new Cloning();), which causes the initialization of the instance variable c (Cloning c=new Cloning();), which creates another Cloning instance, and so on...

You have an infinite chain of constructor calls, which leads to StackOverflowError.

In the above code I have a simple class and a class level instance

You don't have a class level instance. You have an instance level instance. If you want a class level instance, change

Cloning c=new Cloning();

to

static Cloning c=new Cloning();

Why java.lang.StackOverflowError: null when I try to print my entity

The StackOverflowError is because you are(implicitely) calling Utilisateur#toString from Terrain#toString that call Utilisateur#toString that call Terrain#toString that call Utilisateur#toString that call Terrain#toString ...

Edit; It is not Terrain but Reservation as said in the comment below.

TomCat Servlet execution java.lang.StackOverflowError exception

java.lang.StackOverflowError according to the docs

Thrown when a stack overflow occurs because an application recurses too deeply.

From the stack trace, the recursion can be seen in lines below. ElemTemplate.execute calls TransformerImpl.executeChildTemplates which in turns calls the former thus establishing the recursion:

org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
org.apache.xalan.templates.ElemTemplate.execute(ElemTemplate.java:394).

Something in the xml estructure or the code parsing it might be causing the java.lang.StackOverflowError.

Could be worth to check also -Xss jvm setting.

Recursive method causes java.lang.StackOverflowError

It seems your code is going into an infinite loop as you have a wrong condition for the inner loop. The inner loop is iterating and filling up the stack memory, eventually exceeding the amount allocated by the JVM.

In order to avoid this stack overflow error and and perfect the shape of your Pascal's Triangle you can simply add one extra loop and change the condition for the inner loop.

public static void printTriangle(int n) {
for (int row = 0; row < n; row++) {
//Added Spacer loop for getting perfect shape for pascal triangle
for (int spacer = n; spacer > row; spacer--) {
System.out.print(" ");
}
for (int col = 0; col <= row; col++) {
System.out.print(pascalTriangle(row, col) + " ");
}
System.out.println();
}
}


Related Topics



Leave a reply



Submit