Using Java for so long, summarize the common memory overflow problem in Java and the corresponding solution
Heap Overflow error message
Java.lang.OutOfMemoryError:Java Heap Space
Cause of error
- In the heap (the new generation and the old age) can not continue to allocate objects;
- References to certain objects are held for a long time and are not released and the garbage collector cannot be recycled;
- A large number of Finalizer objects are used, and these objects are not within the GC's recycling cycle.
Solutions
- Dump the heap memory and analyze it with MAT to resolve the memory leak;
- If there is no memory leak, use-XMX to increase heap memory;
- If you have a custom Finalizable object, consider the necessity of its existence.
GC Overload overflow error message
Java.lang.OutOfMemoryError:GC Overhead limit exceeded
Cause of error
The garbage collector takes more than 98% of the time to do garbage collection, but reclaims less than 2% of the heap memory.
Solutions
- Add-xx:-usegcoverheadlimit This start parameter to remove the alarm, but this is only a deceiving way, generally appear GC overhead limit exceeded description from the real OOM is not far away;
- Dump the heap memory and analyze it with MAT to resolve the memory leak;
- If there is no memory leak, use-XMX to increase heap memory;
Permanent generation/Meta space overflow error message
Java.lang.OutOfMemoryError:PermGen space or Java.lang.OutOfMemoryError:Metaspace (JAVA8 and above)
Cause of error
The permanent generation is the concrete implementation of the Hotsot virtual machine to the method area, storing the class information, constants, static variables, JIT compiled code, etc. that have been loaded by the virtual machine. It is important to note that after Java8, the permanent generation has a new name: Meta space, which uses local memory. There have also been several changes in the information that exists in the permanent generation:
- String constants are transferred from the permanent generation to the heap;
- The JVM parameters associated with the permanent generation have been removed.
There are several possible reasons for a permanent or meta-space overflow:
- There are frequent constant pool operations (eg. String.intern), this condition applies only to Java7 prior to application;
- Loads a lot of class information, and does not unload in time;
- There is no restart after the application has been deployed.
Solutions
The reason for the permanent generation/meta-space overflow is simple, there are several ways to solve it:
- Application before Java8: use-xx:maxpermsize to increase the size of the permanent generation ();
- Java8 and later applications: If-xx:maxmetaspacesize is set, resize it or remove the parameter.
- Try restarting the JVM.
Method Stack Overflow error message
New native Thread
Cause of error
When the virtual machine expands the stack space, it cannot request enough memory space. A scenario that typically occurs when the memory space is too small, but creates a large number of threads.
Solutions
- By-XSS reducing the capacity of each thread stack size, note the impact of-XMS,-XMX;
- The total number of threads is also limited by the system's free memory and operating system to check if this limit is available under the system:
- /proc/sys/kernel/pid_max,
- /proc/sys/kernel/thread-max,
- Max_user_process (Ulimit-u),
- /proc/sys/vm/max_map_count
Array allocation overflow error message
java.lang.OutOfMemoryError:Requested array size exceeds VM limit
This is typically caused by unreasonable array allocation requests, eliminating code logic errors, or resizing the heap.
Swap partition overflow error message
Java.lang.OutOfMemoryError:Out of swap Space
This is typically caused by the operating system, possibly due to:
- The swap partition size is not allocated enough;
- Other processes on the machine consume all the memory.
Local method Overflow error message
Java.lang.OutOfMemoryError:stack_trace_with_native_method
This situation indicates that a memory allocation failure occurred at run time on the local method. Unlike the java.lang.OutOfMemoryError:unable to create new native Thread save, a method stack overflow occurs at the code level of the JVM, while a local method overflow occurs at the JNI code or local method.
Summary of Java memory overflow issues