JVM配置
-XX:MaxDirectMemorySize=100M -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDetails
测试代码
package com.example.oom; import sun.misc.Unsafe; import java.lang.reflect.Field; public class DirectMemoryOOM { private static int _1MB=1024*1024; public static void main(String[] args) throws Exception { Field unsafeField= Unsafe.class.getDeclaredFields()[0]; unsafeField.setAccessible(true); Unsafe unsafe=(Unsafe)unsafeField.get(null); while (true) { unsafe.allocateMemory(_1MB); } } }
测试结果
Exception in thread "main" java.lang.OutOfMemoryError at sun.misc.Unsafe.allocateMemory(Native Method) at com.example.oom.DirectMemoryOOM.main(DirectMemoryOOM.java:16)
也没有dump,因为这个不是队内oom