This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

A StackOverflowError.

1.1 the bug

public class StackOverflowErrorDemo {
    public static void main(String[] args) {
        javaKeeper();
    }

    private static void javaKeeper(a) { javaKeeper(); }}Copy the code

The JVM stack is deep, and the execution of the method will be accompanied by the loading and unloading of the stack, as seen in the above method, after the execution of the main method recursive, sooner or later, the stack burst

Exception in thread "main" java.lang.StackOverflowError
	at oom.StackOverflowErrorDemo.javaKeeper(StackOverflowErrorDemo.java:15)
Copy the code

1.2 Cause Analysis

  • Infinite recursive loop calls (the most common reason), always watch out for situations in your code where you can’t exit from a loop calling a method
  • A large number of methods are executed, causing the thread stack space to run out
  • A large number of local variables are declared in the method
  • Native code is on the stack allocation of logic, and memory is not small, such as java.net.SocketInputStream.read0 requirements allocated on the stack will be a 64 KB cache (64 – bit Linux)

1.3 Solution

  • Fix the abnormal code that causes infinite recursive call, through the exception stack thrown by the program, find out the line of code that keeps repeating, follow the picture, fix infinite recursive Bug
  • Check for cyclic dependencies between classes (this exception is also raised when two objects reference each other and the toString method is called)
  • Boot parameters through the JVM-XssIncrease the thread stack memory space. Some normal usage scenarios require a large number of methods to be executed or contain a large number of local variables, and the thread stack space limit can be raised appropriately

Java Heap Space

The Java heap is used to store object instances. As long as we keep creating objects and ensure that GC Roots have a reachable path to the objects, we can avoid GC cleaning these objects. As the number of objects increases, the total capacity of the heap will reach the maximum capacity of the heap.

The OOM exception of Java heap memory is the most common out-of-memory exception in practical applications.

2.1 the bug

/** * JVM arguments: -xmx12m */
public class JavaHeapSpaceDemo {

    static final int SIZE = 2 * 1024 * 1024;

    public static void main(String[] a) {
        int[] i = new int[SIZE]; }}Copy the code

The code is trying to allocate a 2M int array. If you specify the startup parameter -xmx12m, the allocated memory is insufficient, which is similar to stuffing an object of size XXXL into the Java Heap space of size S.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at oom.JavaHeapSpaceDemo.main(JavaHeapSpaceDemo.java:13)
Copy the code

2.2 Cause Analysis

  • Request to create a very large object, usually a large array
  • Unexpected traffic/data volume, usually a spike in upstream system requests, is common in various promotional/kill activities, and can be combined with business traffic indicators to check for spikes
  • Overusing finalizers, this object is not immediately GC
  • A Memory Leak, in which a large number of object references are not released and cannot be automatically reclaimed by the JVM, is common when resources such as files are used and not reclaimed

2.3 Solutions

For most cases, it is usually just a matter of scaling up the JVM heap memory space with the -xmx parameter. If the problem persists, refer to the following conditions for further handling:

  • If the object is very large, you can check its reasonableness, such as whether all the results of the database are queried at once without limiting the number of results
  • In case of peak service pressure, add machine resources or perform traffic limiting degradation.
  • If there is a memory leak, you need to find the holding object and modify the code design, such as closing the unfreed connection

Memory leaks and memory overflows

Out of memory refers to the fact that the program does not have enough memory space to use when applying for memory. For example, if you request an Integer but store it for a Long, you are out of memory.

Memory leak refers to a program that cannot release the allocated memory space after applying for it. The damage caused by a memory leak can be ignored, but the accumulation of memory leak can be serious. No matter how much memory is leaked, it will be exhausted sooner or later.

Memory leak will eventually lead to out of memory!