- 直接内存泄漏
- 通过指定最大直接内存-XX:MaxDirectMemorySize=10M,不断申请直接内存,以此发生内存泄漏
- 通过反射获取Unsafe类并通过其分配直接内存
/**
* jvm直接内存溢出
* JVM参数:-Xmx20M -XX:MaxDirectMemorySize=10M
*/
public class DirectMemoryOOM {
private static final int _1MB = 1024 * 1024;
public static void main(String[] args) throws IllegalAccessException {
//通过反射获取Unsafe类并通过其分配直接内存
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
while (true) {
unsafe.allocateMemory(_1MB);
}
}
}
详细代码 github