Java students, more or less will encounter memory overflow (OOM) scene, but the cause of OOM is a variety of reasons.

Get straight to the point:

Stack overflow

This scenario is the most common, with an error message:

java.lang.OutOfMemoryError: Java heap spaceCopy the code

why

(1) There may be large object allocations in the code; (2) there may be memory leaks so that after multiple GC’s, a chunk of memory large enough to hold the current object cannot be found.

The solution

1, check whether there is a large object, the distribution of the most likely to allocate large arrays is 2, through the jmap command, the heap memory dump down, use the mat tool analyze, check whether there is a memory leak problem 3, if not found significant memory leaks, -xmx heap memory 4, it is also easy to be ignored, Check if there are a large number of custom Finalizable objects, possibly provided within the framework, and consider the necessity of their existence

Permanent generation/meta-space overflow

Error message:

java.lang.OutOfMemoryError: PermGen spacejava.lang.OutOfMemoryError: MetaspaceCopy the code

why

The permanent generation is the specific implementation of the HotSot virtual machine to the method area, which stores the virtual machine loaded class information, constants, static variables, JIT compiled code, and so on.

After JDK8, the permanent generation is replaced by the metaclass, which uses local memory, among other details:

  • String constants are moved from the permanent generation to the heap
  • JVM parameters associated with persistent generation have been removed

Possible causes are as follows:

Before Java7, the String. Intern () method was frequently used incorrectly. 2

Failure to restart the JVM process usually occurs during debugging, as shown in the following FAQ:

Why does the memory usage increase when I redeploy a web application?

That is because your web application has a memory leak.

A common issue is “PermGen” memory leaks. They happen because the Classloader (and the Class objects it loaded) cannot be recycled unless some requirements are met (). They are stored in the permanent heap generation by the JVM, and when you redeploy a new class loader is created, which loads another copy of all these classes. This can cause OufOfMemoryErrors eventually.

(*) The requirement is that all classes are loaded by this classloader should be able to be gc ‘ed at The same time.

The solution

Because the OOM is relatively simple, the solutions are as follows:

Check whether the permanent generation space or meta space setting is too small 2. Check whether there are a lot of reflection operations in the code 3

GC overhead limit exceeded

This exception is rare, and the error message is:

Java. Lang. OutOfMemoryError: GC overheadlimit exceededCopy the code

why

This is a new type of error in JDK6, usually caused by the heap being too small. Sun officially defines this as throwing an exception when more than 98% of the time is spent GC and less than 2% of the heap is reclaimed.

The solution

1. Check if there are a lot of dead loops or code that uses a lot of memory in the project and optimize the code.

2, add parameters – XX: – UseGCOverheadLimit disable this check, this parameter actually doesn’t solve the problem of memory, just put the wrong information delay, end up Java. Lang. OutOfMemoryError: Java heap space.

3. Dump the memory and check whether there is any memory leak. If not, expand the memory.

Method stack overflow

Error message:

java.lang.OutOfMemoryError : unable to create new native ThreadCopy the code

why

This kind of exception is basically caused by the creation of a large number of threads, encountered once before, through jStack out of a total of more than 8000 threads.

The solution

The total number of threads is also limited by the free memory of the system and the operating system. Check whether the system has this limit:

  • /proc/sys/kernel/pid_max
  • /proc/sys/kernel/thread-max
  • Maxuserprocess (ulimit -u)
  • /proc/sys/vm/maxmapcount

Unconventional overflow

Below these OOM abnormal, probably most of the students have not encountered, but still need to know about it

Allocating large arrays

Error message:

java.lang.OutOfMemoryError: Requested array size exceeds VM limitCopy the code

This is typically caused by improper array allocation requests, and the JVM performs a check before allocating memory to the array. Addressable specifies whether the array to be allocated is addressable on the platform. If addressable is not, this error is thrown.

The solution is to check your code to see if there are places to create large arrays.

Swap the overflow

Error message:

java.lang.OutOfMemoryError: Out of swap spaceCopy the code

This situation is generally caused by the operating system, the possible reasons are as follows:

1. The size of the swap partition is insufficient.

2. Other processes consume all memory.

Solution:

2. Increase the size of swap partition or machine memory

Local method overflow

Error message:

java.lang.OutOfMemoryError: stack_trace_with_native_methodCopy the code

Local methods failed to allocate memory at runtime, unlike previous method stack overflows, which occurred at the JVM code level, where local method overflows occurred at JNI code or local methods.

This abnormal occurrence probability is very low, can only be diagnosed through the operating system local tools, a little difficult, or give up better.


Have no matter when still read a book more, learn a bit of knowledge more, encounter a problem or not to be solved later?

Check the home page