What causes java.lang.StackOverflowError

What can cause java.lang.StackOverflowError ? The printout of the stack that I get is not very deep (only 5 methods).

+82
java callstack stack-overflow
Jul 07 '10 at 18:26
source share
10 answers

Check out any calls for using the methods. This is mainly caused when there is a recursive method call. A simple example:

 public static void main(String... args) { Main main = new Main(); main.testMethod(1); } public void testMethod(int i) { testMethod(i); System.out.println(i); } 

Here is System.out.println (i); will be pushed onto the stack many times when the testMethod method is called.

+57
Jul 08 '13 at 10:01
source share

One of the (optional) JVM arguments is the size of the stack. This is -Xss. I do not know what the default value is, but if the total number of things on the stack exceeds this value, you will get this error.

Typically, the cause is infinite recursion, but if you see this, your stack trace will have more than 5 frames.

Try adding the -Xss argument (or increasing the value of one) to see if this has disappeared.

+21
Jul 07 '10 at 18:29
source share

What actually causes java.lang.StackOverflowError is usually unintentional recursion. For me, this is often when I intended to call a super method for the overidden method. For example, in this case:

 public class Vehicle { public void accelerate(float acceleration, float maxVelocity) { // set the acceleration } } public class SpaceShip extends Vehicle { @Override public void accelerate(float acceleration, float maxVelocity) { // update the flux capacitor and call super.accelerate // oops meant to call super.accelerate(acceleration, maxVelocity); // but accidentally wrote this instead. A StackOverflow is in our future. this.accelerate(acceleration, maxVelocity); } } 

First, it’s useful to know what happens behind the scenes when we call a function. The arguments and address of where the method was called are pushed onto the stack (see http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Runtime_memory_management ) so that the called method can access the arguments so that the called method completed, execution may continue after the call. But since we call this.accelerate (acceleration, maxVelocity) recursively (there is no recursion when the method is called. For more information see http://en.wikipedia.org/wiki/Recursion_(computer_science) ), we are in a situation, known as infinite recursion, and we continue to add arguments and return the address in a call stack. Since the call stack is finite in size, we end up ending up in space. Starting space on the call stack is called overflow. This is due to the fact that we are trying to use more stack space than ours, and the data literally overflows the stack. In the Java programming language, this throws the java.lang.StackOverflow runtime exception and stops the program immediately.

The above example is somewhat simplified (although this happens to me more than I would admit). The same thing can happen in a more circular way, which makes tracking difficult. However, as a rule, StackOverflow is usually easily resolved as soon as this happens.

In theory, a stack overflow without recursion is also possible, but in practice this will be a rather rare event.

+10
Jun 20 '13 at 3:35
source share

What is java.lang.StackOverflowError

java.lang.StackOverflowError error indicating that the application stack has been exhausted due to deep recursion, i.e. Your program / script is repeating too deeply.

the details

StackOverflowError extends the VirtualMachineError class which indicates that the JVM has run out or has run out of resources and cannot continue to run. VirtualMachineError which extends the Error class is used to indicate those serious problems that the application should not catch. A method may not report such errors in its throw clause because these errors are abnormal conditions that were never expected.

Example

Minimal, Complete, and Verifiable Example :

 package demo; public class StackOverflowErrorExample { public static void main(String[] args) { StackOverflowErrorExample.recursivePrint(1); } public static void recursivePrint(int num) { System.out.println("Number: " + num); if(num == 0) return; else recursivePrint(++num); } } 

Console exit

 Number: 1 Number: 2 . . . Number: 8645 Number: 8646 Number: 8647Exception in thread "main" java.lang.StackOverflowError at java.io.FileOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at java.io.PrintStream.write(Unknown Source) at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source) at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source) at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source) at java.io.OutputStreamWriter.flushBuffer(Unknown Source) at java.io.PrintStream.newLine(Unknown Source) at java.io.PrintStream.println(Unknown Source) at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:11) at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16) . . . at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16) 

Explaination

When a function call is called by a Java application, a stack frame is allocated on the call stack . stack frame contains the parameters of the called method, its local parameters and the return address of the method. The return address indicates the point of execution from which the execution of the program should continue after the return of the called method. If there is no space for the new stack frame, StackOverflowError a Java Virtual Machine (JVM).

The most common case that can run out of Java application stacks is recursion. In recursion, a method calls itself at runtime. Recursion one of the most powerful general-purpose programming methods, but should be used with care to avoid a StackOverflowError .

Recommendations

+5
Dec 15 '17 at 11:29
source share

When a function call is called by the Java application, the stack stack is allocated in the call stack. The stack frame contains the parameters of the called method, its local parameters and the return address of the method.

The return address indicates the point of execution from which the program should continue after the called method returns. If there is no space for a new stack, then a StackOverflowError is raised by the Java Virtual Machine (JVM) .

The most common case that can run out of Java application stacks is recursion.

Please take a look

How to solve StackOverflowError

+4
Sep 28 '15 at 12:05
source share

I created a program with hibernate, in which I created two POJO classes, both with each other's object and with data elements. When in the main method I tried to save them to the database, I also got this error.

This is because both classes reference each other, so a loop is created that causes this error.

So, check if there are any similar relationships in your program.

+2
Apr 3 '14 at 18:40
source share

Exceptions may occur when the stack of threads continues to grow in size until the maximum limit is reached.

Configuring stack parameters (Xss and Xmso) ...

I suggest you look at this link: http://www-01.ibm.com/support/docview.wss?uid=swg21162896 There are many possible reasons for StackOverflowError, as you can see in the link ....

+1
Sep 02 '17 at 12:05
source share

Try to clean the project before compilation.

when compiling and in case of a sudden memory failure, this may cause this error ...

0
Apr 08 '16 at 13:08 on
source share

In my case, I have two activities. In the second exercise, I forgot to put super on the onCreate method.

 super.onCreate(savedInstanceState); 
0
Feb 19 '17 at 17:58
source share

Solution for Hibernate users when parsing data:

I had this error because I was analyzing a list of objects displayed on both sides of @OneToMany and @ManyToOne in json using Jackson, which caused an infinite loop.

If you are in the same situation, you can solve this problem with the @JsonManagedReference and @JsonBackReference .

API definitions:

  • JsonManagedReference ( https://fasterxml.imtqy.com/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonManagedReference.html ):

    An annotation used to indicate that an annotated property is part of a two-way relationship between fields; and that its role is a “parent” (or “direct”) link. The value type (class) of the property must have a single compatible property annotated with JsonBackReference. The binding is processed so that the property annotated with this annotation is processed normally (usually serialized, no special processing for deserialization); this is a backlink that requires special handling

  • JsonBackReference: ( https://fasterxml.imtqy.com/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonBackReference.html ):

    An annotation used to indicate that a related property is part of a two-way relationship between fields; and that its role is a "child" (or "backward") link. The type of the property value must be a bean: it cannot be a collection, map, array, or enumeration. The binding is processed so that the property annotated with this annotation is not serialized; and during deserialization, its value is set equal to the instance that has the “managed” (direct) link.

Example:

Owner.java:

 @JsonManagedReference @OneToMany(mappedBy = "owner", fetch = FetchType.EAGER) Set<Car> cars; 

Car.java:

 @JsonBackReference @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "owner_id") private Owner owner; 

Another solution is to use @JsonIgnore which will simply set the value to zero in the field.

0
Jun 06 '19 at 12:07 on
source share



All Articles