What can cause a java.lang.StackOverflowError
? The stack printout that I get is not very deep at all (only 5 methods).
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Stack Overflow exceptions can occur when a thread stack continues to grow in size until reaching the maximum limit.
Adjusting the Stack Sizes (Xss and Xmso) options...
I suggest you see this link: http://www-01.ibm.com/support/docview.wss?uid=swg21162896 There are many possible causes to a StackOverflowError, as you can see in the link....
Check for any recusive calls for methods. Mainly it is caused when there is recursive call for a method. A simple example is
Here the System.out.println(i); will be repeatedly pushed to stack when the testMethod is called.
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.
Please Have a look
How to solve StackOverflowError
I created a program with hibernate, in which I created two POJO classes, both with an object of each other as data members. When in the main method I tried to save them in the database I also got this error.
This happens because both of the classes are referring each other, hence creating a loop which causes this error.
So, check whether any such kind of relationships exist in your program.
What is
java.lang.StackOverflowError
The error
java.lang.StackOverflowError
is thrown to indicate that the application’s stack was exhausted, due to deep recursion i.e your program/script recurses too deeply.Details
The
StackOverflowError
extendsVirtualMachineError
class which indicates that the JVM have been or have run out of resources and cannot operate further. TheVirtualMachineError
which extends theError
class is used to indicate those serious problems that an application should not catch. A method may not declare such errors in itsthrow
clause because these errors are abnormal conditions that was never expected to occur.An Example
Minimal, Complete, and Verifiable Example
:Console Output
Explaination
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, theStackOverflowError
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
one of the most powerful general-purpose programming technique, but must be used with caution, in order for theStackOverflowError
to be avoided.References
What actually causes a java.lang.StackOverflowError is typically unintentional recursion. For me it's often when I intended to call a super method for the overidden method. Such as in this case:
First, it's useful to know what happens behind the scenes when we call a function. The arguments and the address of where the method was called is pushed on the stack (see http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Runtime_memory_management) so that the called method can access the arguments and so that when the called method is completed, execution can continue after the call. But since we are calling this.accelerate(acceleration, maxVelocity) recursively (recursion is loosely when a method calls itself. For more info see http://en.wikipedia.org/wiki/Recursion_(computer_science)) we are in a situation known as infinite recursion and we keep piling the arguments and return address on the call stack. Since the call stack is finite in size, we eventually run out of space. The running out of space on the call stack is known as overflow. This is because we are trying to use more stack space than we have and the data literally overflows the stack. In the Java programming language, this results in the runtime exception java.lang.StackOverflow and will immediately halt the program.
The above example is somewhat simplified (although it happens to me more than I'd like to admit.) The same thing can happen in a more round about way making it a bit harder to track down. However, in general, the StackOverflow is usually pretty easy to resolve, once it occurs.
In theory, it is also possible to have a stack overflow without recursion, but in practice, it would appear to be a fairly rare event.