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

时间:2017-02-21 10:11:20

标签: java out-of-memory dump

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

应用:     import 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崩溃,智能创意控制台输出:

#
# 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'

我想知道是否与“无法编写核心转储”有关。默认情况下,在客户端版本的Windows上未启用小型转储,是否可能未正确配置Windows系统以转储文件。 我还尝试通过高级系统设置启用小型转储 - >初创公司恢复 - >设置 - >选择'小内存转储'。但没有改变。 有人能给出一些建设性的建议吗? THX!

2 个答案:

答案 0 :(得分:0)

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

如果您想为客户端版本的Java使用minidump,请将-XX:+CreateMinidumpOnCrash添加到命令行。

答案 1 :(得分:0)

事实证明我没有按照正确的顺序放置JVM选项。错误的版本是:java DirectMemoryOOM -Xmx20M -XX:MaxDirectMemorySize = 10M -XX:+ HeapDumpOnOutOfMemoryError -XX:HeapDumpPath = C:\ Users \ chao.zhang1 \ Desktop \ aaa.hprof,这是不对的,因为我们需要将JVM选项放在应用程序类之前,如下所示:java -Xmx20M -XX:MaxDirectMemorySize = 10M -XX:+ HeapDumpOnOutOfMemoryError -XX:HeapDumpPath = C:\ Users \ chao.zhang1 \ Desktop \ aaa.hprof DirectMemoryOOM