Stability topic | StackOverFlowError common causes and solutions

REVIEW

"StabilityGuide" is a volatile area than Ali Ali engineers co-sponsored open source project repository, covering the performance of the pressure test, failure drill, JVM, application container, service framework, traffic scheduling, monitoring, diagnostic and other technical fields, in a more structured way to build the knowledge base in the field of stability, you are welcome to join.

@GitHub :github.com/StabilityMa…

Each JVM thread has a private JVM thread stack for storing the current thread's stack frame JVM (including the called function parameters, local variables and return addresses, etc.). If the thread is a thread stack space is exhausted, there is not enough resources are allocated to the newly created stack frame is thrown java.lang.StackOverflowError error.

How to thread stack is running?

Firstly, a simple procedure call code example, as follows:

public class SimpleExample {
      public static void main(String args[]) {
            a();
      }
      public static void a() {
            int x = 0;
            b();
      }
      public static void b() {
            Car y = new Car();
            c();
      }
      public static void c() {
            float z = 0f;
      }
}
复制代码

After the main () method is called, the method executes the thread A code execution order, it is being executed, the basic data types, object pointer and the return value is wrapped in the stack frame, individually pressed into its private call stack, the overall execution As shown below:

First, the program starts, main () method stack.

Then, A () method of the stack, the variable x is declared as type int, initialization values ​​0. Note that both x or 0 are contained in the stack frame.

Next, B () method of the stack, a Car object is created and is assigned to the variable y. Please note that the actual Car object is created in the Java heap memory instead of thread stack, only Car object reference variables y and is included in the stack frame.

Finally, C () method of the stack, the variable z is declared as type float, the initialization assigned 0f. Similarly, z or 0f are included in the stack frame.

When the method of execution, all the thread stack frames one by one from the stack in the order of LIFO, until the stack is empty.

How StackOverFlowError is produced?

As described above, the JVM thread stack stored procedure to execute a method, the basic data types, local variables, return values, and object pointer information, these need to consume memory. Once a thread stack size increased by more than the allowed limit of memory, it throws java.lang.StackOverflowError error.

The following code through an infinite recursive calls eventually led to java.lang.StackOverflowError error.

public class StackOverflowErrorExample {
      public static void main(String args[]) {
            a();
      }
      public static void a() {
            a();
      }
}
复制代码

In this case, A () method of the infinite stack, stack overflow until depletion thread stack space, as shown in FIG.

Exception in thread "main" java.lang.StackOverflowError
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
    at StackOverflowErrorExample.a(StackOverflowErrorExample.java:10)
复制代码

How to solve StackOverFlowError?

There are several common cause of StackOverFlowError:

  • Infinite recursive loop call (most common).
  • The implementation of a number of methods, causes the thread stack space is exhausted.
  • Local variables declared within a method of mass.
  • native code allocated on the stack logic, and memory requirements is not small, such java.net.SocketInputStream.read0 will require a buffer assigned to 64KB (64 Linux) on the stack.

In addition to procedural errors thrown StackOverflowError outside, there are two positioning methods stack overflow:

  • Process suddenly disappeared, but left a crash log, you can check the stack range of the current thread crash log, and the value of the RSP register. If the value is out of the register stack RSP range, it shows a stack overflow.
  • If you do not crash log, it would only be analyzed by coredump. File before the process is running, execute ulimit -c unlimited, when the hang process, will produce a core. [Pid], and then by jstack $ JAVA_HOME / bin / java core. [Pid] look at the stack output. If the normal output, and it can see if there is a long thread call stack, of course, there may be no normal output, because of this jstack grab a stack of papers from the core command is actually Serviceability Agent-based implementation, while SA in some versions, there Bug.

Common solutions include the following:

  • Fix the code throws an exception infinite recursive calls, exceptions thrown by the program stack to find out repeated lines of code, what you want to repair infinite recursion Bug.
  • Troubleshooting for circular dependencies between classes.
  • Investigation whether there is a class instance variables in the instance of the current class, and a class.
  • By increasing the thread stack memory -Xss JVM startup parameters, certain scenarios need to perform a large number of normal use or a method comprising a large number of local variables, this time can be appropriately increase the thread stack space limitations, for example, by arranging the -Xss2m space to a thread stack 2 mb.

The default thread stack size depends on the operating system, JVM version, and vendor, a common default configuration as follows:

JVM version The default thread stack size
Sparc 32-bit JVM 512 kb
Sparc 64-bit JVM 1024 kb
x86 Solaris/Linux 32-bit JVM 320 kb
x86 Solaris/Linux 64-bit JVM 1024 kb
Windows 32-bit JVM 320 kb
Windows 64-bit JVM 1024 kb

Tip: The actual production system, can be configured to alert key to log in StackOverFlowError, once discovered, dealt with immediately.

Recommended Tools & Products

ARMS - Ali cloud APM products, support StackOverFlowError abnormal keyword alert

Reference article

Author Information: Xia, GitHub ID @StabilityMan, sea nickname career, Ali cloud ARMS & EagleEye technical experts, joined Alibaba in 2016, he has been engaged in work related to link tracking and diagnostics of APM monitoring.


Description link

This article Yunqi community original content may not be reproduced without permission.


Guess you like

Origin juejin.im/post/5d53ca9ef265da03ca11646d