Stack Overflow exceptions can occur when a thread stack continues to grow in size until reaching the maximum limit.
A stack overflow can result from:
- A deeply nested application
- An infinite loop within an application
- A problem in just-in-time (JIT) compiled code
- Applications requiring a larger stack size, especially ones relying on XML, GUI, or java2D classes.
- Native method calls
Stack overflow issues are frequently masked by Out of Memory exceptions. By resolving the memory constraints, the stack overflow can be resolved.
When a stack overflow occurs, the amount of stack space required by the program exceeds what is configured for the stack in the Java™ Virtual Machine (JVM) process, or the native stack size configured by the operating system.
Some applications require stacks that are larger than the default size; for example, a graphics-intensive Java program can require a larger stack, which may require an increase in the stack size to avoid StackOverflow.
Diagnosing The Problem
Look for either Out of Memory messages or java.lang.StackOverflow in the server logs. The process may continue to run after either of these messages are seen.
If a crash did occur, a javacore should have generated on IBM SDK. You will either see the signal as SIGSEGV, SIGILL, SIGABRT, or SIGBUS. Usually the current thread will indicate the following
Resolving The Problem
Detailed Configuration Options
|Explanations and Solutions|
If an application is performing recursion, the maximum stack size can easily be reached and a Stack Overflow exception is thrown. The thread stack has a limited size and eventually its space will run out as the thread stack grows without bounds.
Some traits of recursion:
- Large thread stacks that appear to repeat
- An infinite loop that continuously spawns off threads
- Very large XML documents loaded into the Document Object Model (DOM)
- JSP or servlets calling itself (usually by executing forward or include to itself)
- Repeated calls in native functions
Increasing the thread stack size allows for larger thread stacks. However if the recursion continues to trigger a stack overflow, the next step is to identify what code is causing the recursion from javacores, thread dumps, or even system core files.
A thread stack that indicates it's too large to display can be an indicator of stack overflow. This is especially true if the stack appears to repeat (such as recursive method calls).
JIT/HotSpot Compiled Code
The JIT/HotSpot compiler (JIT) is designed to speed up the JVM execution times by compiling method calls. This can speed up execution time, but as more aggressive optimizations are used, this can inadvertently cause recursion, resulting in stack overflow or crash. The documents linked below explain how to debug JIT and HotSpot compiler issues:
- JIT Problem Determination for IBM SDK 5.0 and 6.0 using -Xjit
- Debugging HotSpot failures on the Solaris and HP-UX operating environment
Depleted Native Stack
It is almost guaranteed that a crash will occur if the native stack runs out of space. System cores can be used to identify long and possibly recursively looping native thread stacks.
To resolve, increase the native stack size (usually by adjusting the operating system limit for stack size) to accommodate the recursive native calls; however, identifying the recursive native calls will help determine the root cause of the stack overflow.
|Detailed Configuration Options|
Adjusting the Stack Sizes (Xss and Xmso) options
If this does not work, you can adjust the stack sizes. Doubling the stack size is suggested as a first step; however, every thread that the JVM creates will consume memory. Be careful to not exhaust your physical and heap memory resources.
For every Java thread, there are two stacks that are utilized. One is for Java code for method calls, and the other is for native C code (on Solaris and HP-UX, only the native stack is utilized). These are adjustable and can be raised to provide more room on the stacks to prevent an overflow.
- Maximum Thread Stack Size (-Xss)
This parameter controls the stack size of Java method calls (non-native) to track the state of variables. If you find looping code or large stacks that are all calling Java methods and do not make native calls, try raising the stack size by using the generic JVM argument below:
On Solaris and HP-UX systems which use the HotSpot JVM, there is no distinction between native and thread stack sizes. This is the only configurable value for all stack sizes; however, on HP-UX systems, there is another argument used to control stack size of the main method.
While the -Xss controls the stack size of all threads in native memory, -XX:MainThreadStackSize controls the size of the main thread. The main thread's native size will be set to whichever value is higher.
Initial Thread Stack Size (-Xiss)
This is only for distributed platforms (AIX, Linux, Windows)
Adjusts the initial thread stack size that the JVM will start with. The default for all distributed platforms is 2KB. In most cases, you will not need to change this option.
Initial Native Stack Size (-Xmso)
This is only for distributed platforms (AIX, Linux, Windows)
This parameter controls the initial stack size of native (operating system) threads. Java code uses this to process calls made into native libraries, such as JIT or JNI calls. If there is an abundance of calls made on the native stack, adjust the native stack size using this generic JVM argument:
NOTE: The maximum stack size for the operating system is controlled by ulimit -s on UNIX and Linux.
This is a chart of default sizes for the various stack size arguments. This is subject to change due to upgrades of the SDK.
|Initial Thread Stack Size||2KB||2KB||2KB||Not Adjustable||Not Adjustable|
|Maximum Thread Stack Size||256KB (32-bit)|
|Not Adjustable||Not Adjustable|
|Native Stack Size||256KB||256KB||32KB (32-bit)|
Examples of Stack Overflow
These logs may contain references to StackOverflow, alongside other messages. Thread stacks may accompany these messages, indicating if there is a recursive call.
- SystemOut.log example
[3/14/15 3:14:15:926 CST] 31415926 BaseMap W CWOBJ0006W: An exception occurred:
<<Thread stack may follow this message>>
[3/14/15 3:14:15:926 PST] 31415926 SystemErr R Caused by: java.lang.StackOverflowError
In javacores, look for "StackOverflowError" in the thread name entry:
|3XMTHREADINFO "ORB.thread.pool : 19" (TID:10167F78, sys_thread_t:E072BCE0, state:R, native ID:E06BEBA0) prio=5: pending=java.lang.StackOverflowError|
System Dump (processed with jextract and DTFJ Dump Analyzer)
Look for this message near the end of a very large stack. This particular example had 1000 recursively called entries.
|Java stack frames dump is too long: <analyzererror>|
15 June 2018