java.lang.StackOverflowError

java.lang.StackOverflowError


reason:Each JVM thread has a private JVM thread stack, which is used to store the JVM stack frame of the current thread (including parameters of the called function, local variables, return addresses, etc.). If the thread stack space of a thread is exhausted and there are not enough resources allocated to the newly created stack frame, it will be thrownjava.lang.StackOverflowErrormistake.
Sample code:

	@Test
    public void testMain(){
        testStackOverflowError(true);
    }

    public void testStackOverflowError(boolean flag){
        if(flag){
            testStackOverflowError(true);
        }
    }

The above codetestStackOverflowErrorThe method will be called in a wireless loop until Java exhausts the thread stack resources, and the error is reported as follows:

java.lang.StackOverflowError
	at com.example.shiro.demo.NormalTest.testStackOverflowError(NormalTest.java:14)
	at com.example.shiro.demo.NormalTest.testStackOverflowError(NormalTest.java:14)
	……

Solution:
causeStackOverFlowErrorCommon reasons are:

  1. Infinite loop recursive call
  2. A large number of methods are executed at the same time and resources are exhausted.
  3. Declare a large number of local variables in the method
  4. Other ways to consume stack resources

Troubleshooting: In addition to the program throwing errors, you can also use other methods to confirmStackOverFlowError

  1. Process disappears suddenly, but remainscrash log, you can check the stack range of the current thread in the crash log and the value of the RSP register. If the value of the RSP register exceeds the stack range, it means a stack overflow.
  2. If there is no crash log, it can only be analyzed through coredump. Before the process runs, execute ulimit -c unlimited. When the process hangs, a core.[pid] file will be generated, and then use jstack $JAVA_HOME/bin/java core.[pid] to view the output stack. If the output is normal, you can check whether there is a thread with a long call stack. Of course, there may be no normal output, because the jstack command to grab the stack from the core file is actually implemented based on the Serviceability Agent, and SA There are bugs in some versions.

Fix:

  1. Repair the exception code that causes infinite recursive calls. Find the repeated lines of code through the exception stack thrown by the program, follow the diagram to find out, and fix the infinite recursion bug.
  2. Check whether there are circular dependencies between classes
  3. Check whether the current class is instantiated in a class and used as an instance variable of the class
  4. Increase the thread stack memory space through the JVM startup parameter -Xss. Some normal usage scenarios need to execute a large number of methods or contain a large number of local variables. In this case, the thread stack space limit can be appropriately increased. For example, adjust the thread stack space to 2 by configuring -Xss2m. mb

The default size of the thread stack depends on the operating system, Java version, and Java vendor:

JVM versionThread stack size
Sparc 32-bit JVM512K
Sparc 64-bit JVM1024K
x86 Solaris/Linux 32-bit JVM320K
x86 Solaris/Linux 64-bit JVM1024K
Windows 32-bit JVM320K
Windows 64-bit JVM1024K

Original address:https://dzone.com/articles/stackoverflowerror-causes-amp-solutions

Related Posts

SpringBoot integrates Jwt (detailed steps + illustrations)

Tutorial on quickly setting up a java back-end development environment with Docker (detailed graphic and text combination)

graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath(‘dot‘), make sure the Gra

Implement the operations of creating directories and files in java

St. Regis takeaway project 1 + source code

The difference between operating Pandas and Excel tables

About unit testing of JAVA backend

Download and installation of CentOS8

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*