JVM中两类错误StackOverflowError和OOM原因复现
StackoverFlowError
- java.lang.StackOverflowError (线程执行栈,栈溢出)
OutofMemoryError
- java.lang.OutOfMemoryError:java heap space (堆空间不足)
- java.lang.OutOfMemoryError:GC overhead limit exceeded (gc垃圾收集器负载过重,就是gc也回收不了垃圾)
- java.lang.OutOfMemoryError:Direct buffer memory (jvm堆内存之外的物理内存不足)
- java.lang.OutOfMemoryError:unable to create new native thread (线程的创建数量过多导致,内存不足)
- java.lang.OutOfMemoryError:Metaspace (元空间内存不足)
StackoverFlowError错误复现
public static void main(String[] args) {
recursion();
}
public static void recursion(){
recursion();
}
结果:
线程的栈大小默认是1024kb,一旦超出该大小就会产生stackOverFlow
java.lang.OutOfMemoryError:java heap space (堆空间不足)错误复现
配置java程序启动jvm参数
-XX:+PrintGCDetails -XX:InitialHeapSize=5m -XX:MaxHeapSize=5m
public static void main(String[] args) throws Exception {
byte[] bytes = new byte[10 * 1024 * 1024];
}
[GC (Allocation Failure) [PSYoungGen: 1024K->504K(1536K)] 1024K->660K(5632K), 0.0010736 secs] [Times: user=0.05 sys=0.05, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 1528K->504K(1536K)] 1684K->871K(5632K), 0.0012427 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 945K->504K(1536K)] 1312K->967K(5632K), 0.0011252 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 504K->504K(1536K)] 967K->991K(5632K), 0.0008079 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 504K->0K(1536K)] [ParOldGen: 487K->767K(4096K)] 991K->767K(5632K), [Metaspace: 3230K->3230K(1056768K)], 0.0065384 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] 767K->767K(5632K), 0.0004416 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (Allocation Failure) [PSYoungGen: 0K->0K(1536K)] [ParOldGen: 767K->749K(4096K)] 767K->749K(5632K), [Metaspace: 3230K->3230K(1056768K)], 0.0073540 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.xiaoxu.test_error.ErrorTest.main(ErrorTest.java:11)
Heap
PSYoungGen total 1536K, used 118K [0x00000000ffe00000, 0x0000000100000000, 0x0000000100000000)
eden space 1024K, 11% used [0x00000000ffe00000,0x00000000ffe1db68,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 4096K, used 749K [0x00000000ffa00000, 0x00000000ffe00000, 0x00000000ffe00000)
object space 4096K, 18% used [0x00000000ffa00000,0x00000000ffabb670,0x00000000ffe00000)
Metaspace used 3333K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 366K, capacity 388K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError:GC overhead limit exceeded (gc垃圾收集器负载过重,就是gc也回收不了垃圾)
配置java程序启动jvm参数
-XX:+PrintGCDetails -XX:InitialHeapSize=5m -XX:MaxHeapSize=5m
产生此次错误,主要是jvm堆中存在大量对量被引用,gc一直无法回收垃圾,导致gc负载过重
public static void main(String[] args) throws Exception {
ArrayList<Object> list = new ArrayList<>();
while (true){
list.add(new Object());
}
}
值得注意的是:如果是创建字符串常量的话,放在常量池中,常量池位于方法区,对应实现是MetaSpace。对象是放在物理jvm之外的物理内存中
配置程序启动时jvm参数
-XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
int i = 0;
try {
while(true) {
list.add(String.valueOf(++i).intern());
}
} catch (Exception e) {
System.out.println("***************i:" + i);
e.printStackTrace();
throw e;
}
}
java.lang.OutOfMemoryError:Direct buffer memory (jvm堆内存之外的物理内存不足)
导致原因:
写NIO程序经常使用ByteBuffer来读取或者写入数据,这是一种基于通道(Channel)与缓冲区(Buffer)的IO方式,它可以使用Native函数库直接分配堆外内存,然后通过一个存储在Java堆里面的DirectByteBuffer对象作为这块内存的引用进行操作。这样能在一些场景中显著提高性能,因为避兔了在Java堆和Native堆中来回复制数据。
ByteBuffer.allocate(capability) 第一种方式是分配VM堆内存,属于GC管辖范围,由于需要拷贝所以速度相对较慢。
ByteBuffer.allocateDirect(capability) 第二种方式是分配OS本地内存,不属于GC管辖范围,由于不需要内存拷贝所以速度相对较快。
但如果不断分配本地内存,堆内存很少使用,那么JV就不需要执行GC,DirectByteBuffer对象们就不会被回收,这时候堆内存充足,但本地内存可能已经使用光了,再次尝试分配本地内存就会出现OutOfMemoryError,那程序就直接崩溃了。
jvm启动时附带的参数
-XX:+PrintGCDetails -XX:MaxDirectMemorySize=5m
public static void main(String[] args) throws InterruptedException {
System.out.println(String.format("配置的maxDirectMemory: %.2f MB",//
sun.misc.VM.maxDirectMemory() / 1024.0 / 1024));
TimeUnit.SECONDS.sleep(3);
ByteBuffer bb = ByteBuffer.allocateDirect(6 * 1024 * 1024);
}
结果
配置的maxDirectMemory: 5.00 MB
[GC (System.gc()) [PSYoungGen: 5906K->1496K(37888K)] 5906K->1504K(123904K), 0.0019607 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[Full GC (System.gc()) [PSYoungGen: 1496K->0K(37888K)] [ParOldGen: 8K->1359K(86016K)] 1504K->1359K(123904K), [Metaspace: 4182K->4182K(1056768K)], 0.0078596 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
at java.nio.Bits.reserveMemory(Bits.java:693)
at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:311)
at com.xiaoxu.test_error.ErrorTest.main(ErrorTest.java:17)
Heap
PSYoungGen total 37888K, used 874K [0x00000000d6580000, 0x00000000d8f80000, 0x0000000100000000)
eden space 32768K, 2% used [0x00000000d6580000,0x00000000d665a8d8,0x00000000d8580000)
from space 5120K, 0% used [0x00000000d8580000,0x00000000d8580000,0x00000000d8a80000)
to space 5120K, 0% used [0x00000000d8a80000,0x00000000d8a80000,0x00000000d8f80000)
ParOldGen total 86016K, used 1359K [0x0000000083000000, 0x0000000088400000, 0x00000000d6580000)
object space 86016K, 1% used [0x0000000083000000,0x0000000083153e38,0x0000000088400000)
Metaspace used 4213K, capacity 4678K, committed 4864K, reserved 1056768K
class space used 465K, capacity 494K, committed 512K, reserved 1048576K
java.lang.OutOfMemoryError:unable to create new native thread (线程的创建数量过多导致,内存不足)
导致原因:
应用创建了太多线程,一个应用进程创建多个线程,超过系统承载极限
服务器并不允许你的应用程序创建这么多线程**,linux系统默认运行单个进程可以创建的线程为1024个**,如果应用创建超过这个数量,就会报 java.lang.OutOfMemoryError:unable to create new native thread
配置java程序启动jvm参数
-XX:+PrintGCDetails -XX:InitialHeapSize=5m -XX:MaxHeapSize=5m
注意:可能会导致死机
public static void main(String[] args) {
for (int i = 0; ; i++) {
System.out.println("************** i = " + i);
new Thread(() -> {
try {
TimeUnit.SECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, String.valueOf(i)).start();
}
}
java.lang.OutOfMemoryError:Metaspace (元空间内存不足)
元空间主要是方法区的实现,存放常量池,静态变量,类相关信息等
默认的元空间大小为21M左右
通过jvm参数 -XX:PrintFlagsFinal 或者 jinfo -flag MetaspaceSize 查看
一旦元空间满了,要注意元空间中存放的信息