1 Common several OOM
Common OOM errors include:
java.lang.StackOverflowError
java.lang.OutOfMemoryError : java heap space
java.lang.OutOfMemoryError : GC overhead limit exceeded
java.lang.OutOfMemoryError : Direct buffer memory
java.lang.OutOfMemoryError : unable to create new native thread
java.lang.OutOfMemoryError : Metaspace
java.lang.OutOfMemoryError: Out of swap space
java.lang.OutOfMemoryError: Requested array s
There are three reasons for the OOM(OutOfMemoryError) problem:
- Insufficient resources
- Too much memory requested
- Resource depletion
2 java.lang.StackOverflowError
Cause: This error occurs when calling yourself in a function. The function call stack is too deep
//-Xms10m -Xmx10m public class StackOverflowErrorDemo { public static void main ( String [] args) { stackOverflowError(); } private static void stackOverflowError () { stackOverflowError(); / / the Exception in the thread "is the main" Java. Lang. StackOverflowError}} / / console results Exception in the thread "main" java.lang.StackOverflowErrorCopy the code
Solution: 1. Avoid circular recursive calls; 2. Scale up the stack’s Memory; 3.
3 java.lang.OutOfMemoryError : java heap space
Reasons: 1. Creating a large object 2. Exceeding expected volume of visits/data 3. A memory leak
The big object
//-Xms2m -Xmx2m -XX:+HeapDumpOnOutOfMemoryError public class OutOfMemoryErrorJavaHeapSpaceDemo { static final int SIZE = 3 * 1024 * 1024; public static void main(String[] a) { int[] i = new int[SIZE]; }} / / console the Exception in the thread "is the main" Java. Lang. OutOfMemoryError: Java heap spaceCopy the code
Some resources that provide close are not closed and cause memory leaks, such as database links, network links, and IO.
4 java.lang.OutOfMemoryError : GC overhead limit exceeded
Reason: The percentage of time spent doing garbage collection is too large and the effective amount of computation is too small. By default, the JVM throws this error if the GC takes more than 98% of the time and the GC recyls less than 2% of the memory
//-Xmx10M -Xms10m -XX:MaxMetaspaceSize=10M public static void main(String[] args) { List < String > list = new ArrayList < String > (); while (true) { list.add(UUID.randomUUID().toString().intern()); } } //Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceededCopy the code
Solution: 1. Find out which class of objects is using the most memory and see if you want to increase the heap. 2. GC turning is required
5 java.lang.OutOfMemoryError : Direct buffer memory
The reason: This is mainly caused by NIO. NIO programs often use ByteBuffer to read or write data. This is a channel and buffer based I/O method, which can allocate out-of-heap memory directly using native libraries. This is then referenced by a DirectByteBuffer object stored in the Java heap. This can significantly improve performance in some scenarios because it avoids switching back and forth between the JAVA heap and native heap. Bytebuffer.allocate (capability) This method allocates JVM heap memory, which is under GC jurisdiction and is relatively slow because it requires copying
6 java.lang.OutOfMemoryError : unable to create new native thread
Cause: 1. The application creates too many threads. One application process creates multiple threads, which exceeds the system capacity. Your server does not allow your application to create this many threads. The default number of threads created by a single process is 1024 for Linux users
while (true) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(10000000);
} catch(InterruptedException e) {
}
}
}).start();
}
Copy the code
Solution: 1. Find a way to reduce the number of threads your application creates. 2. Expand the Linux default limit for modified Liunx configurations that do require the creation of many threads. Command: ulimit -u view vi/etc/security/limits. D / 90 – nproc. Conf increase a user record configuration size
7 java.lang.OutOfMemoryError : Metaspace
Reason: Since the method area has been moved to Metaspace, the amount of Metaspace used is related to the number/size of classes loaded into memory by the JVM. The main reason is that the Metaspace is used up due to the large or large number of classes loaded into the memory. Instead of persistent generation, Java8 uses meta-space, which is the implementation of method areas in HotSpot. The biggest difference from persistent generation is that the meta-space does not reside in memory in the virtual machine but uses local memory. The meta space stores the following information: 1. Information about classes loaded by VMS. 2 constant pool; Static variables. 4. Just-in-time compiled code
//-Xmx10M -Xms10m -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=10M
public class Metaspace {
static javassist.ClassPool cp = javassist.ClassPool.getDefault();
public static void main(String[] args) throws Exception {
for (int i = 0; ; i++) {
Class c = cp.makeClass("Metaspace.demo.Generated" + i).toClass();
}
}
}
//Exception in thread "main" java.lang.OutOfMemoryError: Metaspace
Copy the code
Considerations: -xx :MaxMetaspaceSize= 512M However, do not limit the size of Metaspace memory, if not enough physical memory, it might cause memory swapping (Considerations), which could seriously affect system performance. In addition, it may cause problems such as native memory allocation failure.
8 java.lang.OutOfMemoryError: Out of swap space
Cause: An OPERATING system-level problem occurs, for example: 1. The swap space configured in the operating system is insufficient. 2. Another process on the system consumes all memory resources. 3. There may also be a local memory leak that causes the application to fail. For example, the application calls Native code to continuously allocate memory, but it is not freed.
Solution: First, upgrading the machine to contain more memory is also the easiest way to increase the size of the swap space. The setup varies from operating system to operating system. Second, optimize your application to reduce its memory footprint
9 java.lang.OutOfMemoryError: Requested array size exceeds VM limit
Cause: The array is too large and the final length exceeds the platform limit but is smaller than integer.max_int
To get to the JVM’s limit on array size, allocate an array whose length is roughly equal to Integer.maxint. When this example runs on 64-bit Mac OS X, Hotspot 7, only two lengths will throw this error: integer.maxint-1 and integer.max_int.
Solution:
1. You need to check your business code to see if you really need that large array. If you can reduce the array size, if not, you may need to split the data into chunks and load them in batches as needed. 2. Modify the program logic. For example, divide into many small pieces and load them in batches; Or abandon the standard library and deal with data structures yourself, such as using the Sun.misc. Unsafe class, which allocates memory as directly as C does.
10 Out of memory:Kill process or sacrifice child
Reason: To understand this error, we need to add a little operating system basics. Operating systems are built on the concept of processes that operate in the kernel, including a very special one called “Out of Memory Killer.” When the kernel detects that the system is out of memory, OOM Killer is activated and a process is selected to kill. Which process is so unlucky? The algorithm and the idea of selection is simple: whoever takes up the most memory gets killed.
The most effective and straightforward way to solve this problem is to upgrade the memory. Other methods include tweaking the OOM Killer configuration, scaling out the application horizontally, and spreading the memory load across several small instances
JVM full directory
Class loading mechanism 3. Runtime data area [PC register, vm stack, local method stack] 4. Runtime data area [heap] 5. Runtime data area [method area] 6. Temporary absence 7. Runtime data area [instantiated memory layout and access location of objects, direct memory] 8. String constant pool 10. Garbage collection [overview, related algorithms] 11. Garbage collection [related concepts] 12. Common OOM 14. JDK command line tools