抛出 OOM 异常时无法使用 VM 选项获取转储文件:HeapDumpOnOutOfMemoryError

can't get a dump file using VM Option :HeapDumpOnOutOfMemoryError when OOM exception thrown

首先,我将 Intelli Idea 用作 IDE 到 运行 旨在导致 OOM 异常的应用程序。 虚拟机选项: -Xmx20M -XX:MaxDirectMemorySize=10M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Users\chao.zhang1\Desktop\aaa.hprof

申请: 导入 sun.misc.Unsafe;

import java.lang.reflect.Field;
public class DirectMemoryOOM{
    private static final 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);
        }
    }
}

抛出异常后,目录 'C:\Users\chao.zhang1\Desktop\' 中不存在转储文件。

其次,我尝试另一种方法来避免IDE的影响,我在cmd命令行中运行这个命令: java DirectMemoryOOM -Xmx20M -XX:MaxDirectMemorySize=10M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Users\chao.zhang1\Desktop\aaa.hprof 仍然没有创建转储文件。

控制台登录为:

Exception in thread "main" java.lang.OutOfMemoryError
        at sun.misc.Unsafe.allocateMemory(Native Method)
        at DirectMemoryOOM.main(DirectMemoryOOM.java:12)

2017/2/23 更新: 今天我运行进入另一个jvm crash,intelli idea控制台输出:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (javaCalls.cpp:62), pid=11864, tid=13164
#  guarantee(thread->is_Java_thread()) failed: crucial check - the VM thread cannot and must not escape to Java code
#
# JRE version: Java(TM) SE Runtime Environment (7.0_79-b15) (build 1.7.0_79-b15)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode windows-amd64 compressed oops)
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# E:\apache-tomcat-7.0.69\apache-tomcat-7.0.69\bin\hs_err_pid11864.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
Disconnected from the target VM, address: '127.0.0.1:58121', transport: 'socket'

我想知道是否与'Failed to write core dump. Minidumps are not enabled by default on client versions of Windows'有关,是否可能windows系统未正确配置转储文件。 我还尝试通过高级系统设置启用小型转储 --> 启动和恢复 --> 设置 --> 选择 'small memory dump'。但是什么都没有改变。 谁能给点建设性的意见?谢谢!

您在系统设置中启用的是蓝屏情况下的小型转储,即当内核模式发生异常时。这与Java.

无关

如果您想要 Java 客户端版本的小型转储,请将 -XX:+CreateMinidumpOnCrash 添加到您的命令行。

原来我没有按正确的顺序排列 JVM 选项。错误的版本是:java DirectMemoryOOM -Xmx20M -XX:MaxDirectMemorySize=10M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Users\chao.zhang1\Desktop\aaa.hprof ,这是不正确的,因为我们需要像这样将 JVM 选项放在应用程序 class 之前:java -Xmx20M -XX:MaxDirectMemorySize=10M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=C:\Users\chao.zhang1\Desktop\aaa.hprof DirectMemoryOOM