What is a StackOverflowError
, what causes it, and how should I deal with them?
相关问题
- 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 means exactly that: a stack overflows. Usually there's a one stack in the program that contains local-scope variables and addresses where to return when execution of a routine ends. That stack tends to be a fixed memory range somewhere in the memory, therefore it's limited how much it can contain values.
If the stack is empty you can't pop, if you do you'll get stack underflow error.
If the stack is full you can't push, if you do you'll get stack overflow error.
So stack overflow appears where you allocate too much into the stack. For instance, in the mentioned recursion.
Some implementations optimize out some forms of recursions. Tail recursion in particular. Tail recursive routines are form of routines where the recursive call appears as a final thing what the routine does. Such routine call gets simply reduced into a jump.
Some implementations go so far as implement their own stacks for recursion, therefore they allow the recursion to continue until the system runs out of memory.
Easiest thing you could try would be to increase your stack size if you can. If you can't do that though, the second best thing would be to look whether there's something that clearly causes the stack overflow. Try it by printing something before and after the call into routine. This helps you to find out the failing routine.
To describe this, first let us understand how local variables and objects are stored.
Local variable are stored in stack:
If you looked at the image you should be able to understand how things are working.
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, to avoid
StackOverflowError
.An example throwing a
StackOverflowError
is shown below:StackOverflowErrorExample.java:
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 until we pass in0
as a parameter. However, in our example, we passed in the parameter from 1 and its increasing followers, consequently 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.How to deal with the 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 512KB, or 1MB. You can increase the thread stack size using the
-Xss
flag. This flag can be specified either via the project’s configuration, or via the command line. The format of the-Xss
argument is:-Xss<size>[g|G|m|M|k|K]
StackOverflowError
is to the stack asOutOfMemoryError
is to the heap.Unbounded recursive calls result in stack space being used up.
The following example produces
StackOverflowError
:StackOverflowError
is avoidable if recursive calls are bounded to prevent the aggregate total of incomplete in-memory calls (in bytes) from exceeding the stack size (in bytes).If you have a function like:
Then foo() will keep calling itself, getting deeper and deeper, and when the space used to keep track of what functions you're in is filled up, you get the stack overflow error.
This is a typical case of
java.lang.StackOverflowError
... The method is recursively calling itself with no exit indoubleValue()
,floatValue()
, etc.Rational.java
Main.java
Result
Here is the source code of
StackOverflowError
in OpenJDK 7Here is an example of a recursive algorithm for reversing a singly linked list. On a laptop with the following spec (4G memory, Intel Core i5 2.3GHz CPU, 64 bit Windows 7), this function will run into StackOverflow error for a linked list of size close to 10,000.
My point is that we should use recursion judiciously, always taking into account of the scale of the system. Often recursion can be converted to iterative program, which scales better. (One iterative version of the same algorithm is given at the bottom of the page, it reverses a singly linked list of size 1 million in 9 milliseconds.)
Iterative Version of the Same Algorithm: