文章目录
- 前言
- 一、基础架构
- 二、常见OOM
-
- 1、栈内存溢出java.lang.StackOverflowError
- [2、堆内存溢出java.lang.OutOfMemoryError:Java heap space](#2、堆内存溢出java.lang.OutOfMemoryError:Java heap space)
- [3、GC回收时间过长java.lang.OutOfMemoryError: GC overhead limit exceeded](#3、GC回收时间过长java.lang.OutOfMemoryError: GC overhead limit exceeded)
- [4、NIO程序堆外内存溢出java.lang.OutOfMemoryError: Direct buffer memory](#4、NIO程序堆外内存溢出java.lang.OutOfMemoryError: Direct buffer memory)
- [5、(重要)不能创建本地线程java.lang.OutOfMemoryError: unable to create native thread](#5、(重要)不能创建本地线程java.lang.OutOfMemoryError: unable to create native thread)
- [6、元空间溢出java.lang.OutOfMemoryError: Metaspace](#6、元空间溢出java.lang.OutOfMemoryError: Metaspace)
前言
对OOM部分的认识,予以记录!
一、基础架构
Error与Exception
二、常见OOM
1、栈内存溢出java.lang.StackOverflowError
方法的递归调用
java
public class StackOverFlowErrorDemo {
public static void main(String[] args) {
stackOverFlowError();
}
private static void stackOverFlowError() {
stackOverFlowError();
}
}
2、堆内存溢出java.lang.OutOfMemoryError:Java heap space
new大对象
java
public class HeapSpaceDemo {
public static void main(String[] args) {
byte[] bytes = new byte[30 * 1024 * 1024];
}
}
3、GC回收时间过长java.lang.OutOfMemoryError: GC overhead limit exceeded
超过98%的时间做GC而回收了不到2%的堆内存
java
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;
}
}
}
4、NIO程序堆外内存溢出java.lang.OutOfMemoryError: Direct buffer memory
netty nio
java
public class DirectBufferMemoryDemo {
public static void main(String[] args) {
System.out.println("配置的maxDirectoryMemory:" + (sun.misc.VM.maxDirectMemory() / (double)1024 / 1024) + "MB");
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(6 * 1024 * 1024);
}
}
5、(重要)不能创建本地线程java.lang.OutOfMemoryError: unable to create native thread
java
package com.kqli.oom;
public class UnableCreateNewThreadDemo {
public static void main(String[] args) {
for (int i = 0; ; i++) {
System.out.println("=========" + i);
new Thread(() -> {
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}, "Thread-" + i).start();
}
}
}
将java文件复制到ubuntu中,执行以下命令:
shell
javac -d . UnableCreateNewThreadDemo.java
java com.kqli.oom.UnableCreateNewThreadDemo