An overview of the

  • Make a note of the OOM situations that may occur in daily development and some solutions

1, heap space OOM

java.lang.OutOfMemoryError: Java heap space
Copy the code
  • The reasons causing
    • You have a really big object, like a collection or an array or something, and you keep adding things to it
    • Memory leak problem, memory cannot be reclaimed
  • performance
    • GC takes longer and longer and runs slower and slower (when no exceptions have been thrown)
  • The solution
    • In the case of sufficient memory Settings, the important thing is to find which class has the problem, and the probability is that it is always not freed at new
    • Dump the current heap by jMAP instruction, using tools to analyze the composition of objects (JvisiOM, MAT) can be
    • Look for the usage flow of that class

2, meta space OOM

java.lang.OutOfMemoryError: PermGen space
java.lang.OutOfMemoryError: Metaspace
Copy the code
  • After JDK8, the original space uses the local memory, which stores the data information of the class
  • Cause:
    • Check to see if you are using too much reflection. Because reflection generates bytecode dynamically, this may cause
    • Check to see if there is a problem with the dynamic proxy. The dynamic proxy also generates bytecode in reverse, which is reflected back to generate the proxy class
  • Performance:
    • Frequent full GC
  • Solution:
    • Also look after dump to see if there is a large number of reflective generated proxy classes
    • Remember, a class is also an object, and that’s pretty much the same as the methods above

3. The VM stack is OOM

java.lang.OutOfMemoryError : unable to create new native Thread
Copy the code
  • The reasons causing
    • The virtual machine stack is thread private, so it only takes enough threads to overflow
    • Inadvertently creating a large number of threads
  • performance
    • Nothing. Just a throw
  • The solution
    • Try to use the thread pool to write, to prevent a large number of threads
    • You need to focus on where you’re recursively creating threads
    • Reduce the size of each thread stack -Xss

4, direct memory OOM

  • The reasons causing
    • Direct memory is not managed in the JVM; it is managed directly by the operating system, so no exceptions are thrown in the JVM
    • The high probability is caused by operations such as NIO that directly request out-of-heap memory
  • performance
    • The virtual machine is down, but Dump is normal. No problem is found
  • The solution
    • Find out if the code that uses NIO directly or indirectly directly requests that out-of-heap memory is not freed