What is OOM? Don't even know it is out of memory? Suggest to give up, I'm too miserable

What is OOM? out of memory

  • Out of memory
  • This error is thrown when the JVM does not have enough memory to allocate space for the object and the garbage collector has no space to reclaim it.

Why is OOM?

That is, why is there no memory?

  • Less allocated: less memory can be used by the virtual machine itself (generally specified by the VM parameter at startup)
  • The application uses too much, and is not released when used up, and is wasted. At this time, memory leaks or memory overflows will occur .

Memory leak : The memory used by the application is not released, causing the memory that the virtual machine can not use again, because the applicant does not use it, and it cannot be allocated to others by the JVM.
Memory overflow : The requested memory exceeds the memory size that the JVM can provide.

java heap overflow

the reason

  • Cannot allocate object in java heap
  • The application retains objects that cannot be recycled by GC
  • The application overuses finalizer

Java heap overflow troubleshooting ideas

1. Find key error messages, such as

java.lang.OutOfMemoryError: Java heap space

2. Use a memory image analysis tool (such as Eclipsc Memory Analyzer or Jprofiler) to analyze the heap storage snapshot from Dump, and analyze whether it is a memory leak or a memory overflow.
3. If it is a memory leak, you can further check the reference chain of the leaked object to the GC Roots through the tool to fix the memory leak in the application.
4. If there is no leak, first check whether the code has infinite loops, recursion, etc., and then consider using -Xmx to increase the heap size

Stack overflow

Regarding the virtual machine stack and the local method stack, two exceptions are described in the Java virtual machine stack specification:

  • If the stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, a StackOverflowError exception will be thrown;
  • If the virtual machine can be dynamically expanded, an OutOfMemoryError exception will be thrown when the maximum memory cannot be applied for when it is expanded.

Cause of stack overflow

  • In the thread, the stack frame is too large, or the virtual machine stack capacity is too small, when the memory cannot be allocated, the virtual machine throws StackOverflowError exception
  • Constantly creating threads will produce exceptions or memory leaks

Knowledge point supplement:
A stack is created with the call of a method, and destroyed after the method is called. Data such as local variables and operand stack are stored in the stack frame.
The Java stack is also called the virtual machine stack. The JVM stack only stores, pushes, and pops the stack frame.
Virtual machine stack

  • Each thread contains a stack area in which only objects of basic data types and references to custom objects are stored in the stack (not objects, objects are stored in the heap area).

Stack overflow troubleshooting ideas

  • Find the key error message and determine whether it is StackOverflowError or OutOfMemoryError
  • If it is StackOverflowError, check whether the code is a recursive call
  • If it is OutOfMemoryError, check whether there is an endless loop to create threads, etc., and reduce the capacity of each thread stack size through -Xss.

Method area overflow

Method area (also called permanent generation, after JDK8, meta space replaced permanent generation), used to store relevant information of Class, such as class name, access modifier, constant pool , field description, method description, etc. A large number of classes will be generated at runtime, which will fill up the method area

Method area overflow reason

  • Use CGLib to generate a large number of proxy classes, causing the method area to be burst
  • Before Java 7, String.intern was frequently used incorrectly
  • A large number of jsp and dynamically generated jsp
  • The application runs for a long time without restarting

Method area overflow troubleshooting ideas

  • Check whether the permanent generation space is set too small
  • Check the code for frequent errors, use the String.intern method
  • Check if it is related to jsp.
  • Check whether a large number of proxy classes are generated using CGLib
  • Restart Dafa, restart JVM

Supplementary knowledge points:

Constant pool in method area:

  • The constant pool is divided into a literal constant pool and a symbolic reference constant pool:
    literal constant (indicating that the value is stored) :
    1. Text string: 1. Text string:
    the value of the defined string constant public String str = "Hello World" ;
    "Hello World" will be stored in the constant pool
    public String str1 = new String("Hello Java");
    The value of "Hello Java" will not be stored in the constant pool, but in the heap memory.
    2. Final type constant value: public final int age = 11, stored in the constant pool after 11
    3. Basic data type value byte, short, int, float, double, long, char, boolean, not all basic The values ​​of the data types are all stored in the constant pool. The four types of values ​​of byte, short, char, and boolean will not be stored in the constant pool.
    Character reference : indicates the storage of the reference.
    1. The fully qualified name of the class or interface: that is, the full path name of the class or interface.
    2. The name and descriptor of the field: the name of the attribute in the class and the description of the attribute.
    3. The name and descriptor of the method.

Guess you like

Origin blog.csdn.net/zl1107604962/article/details/108692520