1, Heap memory overflow exception java.lang.OutOfMemoryError: PermGen space
Causes and recurrence of problems
Use Tomcat 6 to start the project, and the console reports an error
problem analysis
Tomcat 6 finds that the heap memory is insufficient after query. You can adjust the JVM parameters to solve this problem.
The default size of PermGen (permanent generation) is relatively small, 64M. It often occurs when a large number of operating variables are carried out
Solve the cause
Set the appropriate JVM parameters when the project starts
-Xms1024m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=512m
Avoid occurrence
Set appropriate JVM parameters according to the project and your own judgment before starting.
-Xms1024m initial
-Xmx1024m
-XX:PermSize=512m
-XX:MaxPermSize=512m
2, Concurrent modification exception java.util.ConcurrentModificationException
Causes and recurrence of problems
problem analysis
Using ArrayList to perform a large number of add operations in a multithreaded environment will cause concurrent modification exceptions
Solve the cause
List<String> list = new ArrayList<>();
Can be replaced by
List<String> list2 = new Vector<>(); List<String> list3 = Collections.synchronizedList(new ArrayList<>()); List<String> list4 = new CopyOnWriteArrayList();
Avoid occurrence
When a large number of threaded lists are used in a project, consider several other lists
3, Stack overflow java.lang.StackOverflowError
Set VM parameters
-Xms1m -Xmx1m
Fault phenomenon
java.lang.StackOverflowError
Cause
Deep method call
package com.xin; /** * @author : Be careful * @date : Created in 2021/11/6 21:33 * @description: */ public class StackOverFlowError { public static void main(String[] args) { StackOverFlowErrors(); } public static void StackOverFlowErrors(){ StackOverFlowErrors(); } }
4, Heap memory overflow exception java.lang.OutOfMemoryError: Java heap space
Set VM parameters
-Xms1m -Xmx1m
Fault phenomenon
java.lang.OutOfMemoryError: Java heap space
Cause
Java 8, too many objects, too big
public class JavaHeapSpaceDemo { public static void main(String[] args) { String str = "1111"; while (true){ str += str + new Random().nextInt(1111111) + new Random().nextInt(1111111222); str.intern(); } } }
5, Exception java.lang.OutOfMemoryError: GC overhead limit exceeded
configuration parameter
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
Fault phenomenon
java.lang.OutOfMemoryError: GC overhead limit exceeded
Cause
Java.lang.outofmemoryerror will be thrown if the GC recycling time is too long: GC overhead limit exceeded. More than 98% of the time is spent on GC and less than 2% of the heap memory is recycled. Adding no exceptions will cause the memory to fill up quickly every time, resulting in 100% CPU all the time, and GC has no results
public class GCOverHeadDemo { public static void main(String[] args) { int i = 0; List<String> list = new ArrayList<>(); try { while (true) { list.add(String.valueOf(++i).intern()); } }catch (Throwable e){ System.out.println("*****************" + i); e.printStackTrace(); throw e; } } }
6, Direct memory overflow java.lang.OutOfMemoryError: Direct buffer memory
configuration parameter
-Xms10m -Xmx10m -XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
Fault phenomenon
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
Cause
Write NIO program, read and write data through Buffer, which is an I / O mode based on channel (Chanel) and Buffer (Buffer)
It can directly allocate out of heap memory using the native function, and operate through a DirectByteBuffer object stored in the Java heap as a reference to this memory,
This can improve performance in some scenarios, so avoid copying data back and forth between the Java heap and the Native heap.
ByteBuffer.allocate(capability) allocates JVM heap memory in the first way, which belongs to the jurisdiction of GC. It is slow to copy
ByteBuffer.allocateDirect(capability): the first method allocates OS local memory, which is not under the jurisdiction of GC. Because memory copying is not required, it is relatively fast
However, if local memory is constantly allocated and heap memory is rarely used, the JVM does not need to perform GC and the DirectByteBuffer object will not be recycled
At this time, the heap memory is sufficient, but the local memory has been used up. If you try to allocate the local memory again, OutOfMemoryError will appear and the program will crash.
public class DirectBufferMemoryDemo { public static void main(String[] args) { System.out.println("Configured maxDirectMemory" + (sun.misc.VM.maxDirectMemory() /(double) 1024 / 1024) +"MB" ); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace();} ByteBuffer byteBuffer = ByteBuffer.allocateDirect(6 * 1024 * 1024); } }
7, java.lang.OutOfMemoryError: Direct buffer memory
configuration parameter
Fault phenomenon
java.lang.OutOfMemoryError: Direct buffer memory
Cause
When high concurrency requests the server, it is often related to the server
1. Your application creates too many threads. An application creates multiple threads, exceeding the system load limit
2 your server does not allow applications to create multiple threads. The Linux system defaults to a single thread to create threads
If your application creation exceeds this number, it will be reported
terms of settlement
1. Try to reduce the number of threads created by the application, analyze whether the application needs to create multiple threads, not modify the code
2 for some applications, many threads need to be created, far exceeding the default limit of 1024 threads in Linux. The Linux configuration can be modified
Linux
vim /etc/security/limits.d/90-nproc.conf It can be configured according to the actual situation
package com.xin; import java.util.concurrent.TimeUnit; /** * @author : Be careful * @date : Created in 2021/11/7 9:55 * @description: */ public class UnableCreateNewThreadDemo { public static void main(String[] args) { for (int i = 0; ; i++) { System.out.println("****************" + i); new Thread(() -> { try { TimeUnit.SECONDS.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { e.printStackTrace(); } }, String.valueOf(i)).start(); } } }
8, Metaspace exception java.lang.OutOfMemoryError: Metaspace
configuration parameter
-XX:MetaspaceSize=8m -XX:MaxMetaspaceSize=8m
Fault phenomenon
java.lang.OutOfMemoryError: Metaspace
Cause
Java 8 and later versions use Metaspace to replace the permanent generation
Metaspace is implemented in HotSpot. The difference from the persistent generation is that Metaspace is not in the virtual machine memory. Second, it uses local memory, that is, in java8, the class metadata (the virtual machines internal presentation of java class) is stored in the native memory called Metaspace
The permanent generation (replaced by the original space Metaspace after Java 8) stores the following information:
Class information loaded by virtual machine
Constant pool
Static variable
Just in time compiled code
package com.xin; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import java.lang.reflect.Method; /** * @author : Be careful * @date : Created in 2021/11/7 10:42 * @description: */ public class MetaspaceDemo { static class OOMTest{} public static void main(String[] args) { int i = 0; try{ while (true){ i++; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(OOMTest.class); enhancer.setUseCache(false); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { return methodProxy.invokeSuper(o,args); } }); enhancer.create(); } }catch (Throwable t){ System.out.println(i+"An exception has occurred"); t.printStackTrace(); } } }