简体   繁体   English

如何进行堆转储?

[英]How to take heap dump?

I want to collect heap dump on JVM crash我想在 JVM 崩溃时收集堆转储

So i wrote a simple code所以我写了一个简单的代码

public class Test {
private String name;

public Test(String name) {
    this.name = name;
}

public void execute() {

    Map<String,String> randomData = new HashMap<String,String>();
    for(int i=0;i<1000000000;i++) {
       randomData.put("Key:" + i,"Value:" + i);
    }
}

public void addData() {
}

public static void main(String args[]) {
    String myName = "Aniket";
    Test tStart = new Test(myName);
    tStart.execute();
}
}

and I am running it as follows我按如下方式运行它

[aniket@localhost Desktop]$ java -cp . -Xms2m -Xmx2m Test
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Test.execute(Test.java:15)
    at Test.main(Test.java:25)

I got OutOfMemoryError which I wanted but there is no heap dump in the working directory(like hs_err_pidXXXX.log which I expected).我得到了我想要的 OutOfMemoryError 但工作目录中没有堆转储(如我期望的hs_err_pidXXXX.log )。 What am I missing?我错过了什么? How do I get a heap dump?我如何获得堆转储?

Update :更新 :

I tried -XX:ErrorFile=.我试过-XX:ErrorFile=. still no use.还是没用。 If above is not the way to get the heap dump(Crash JVM) how can I crash my JVM to get those logs?如果以上不是获取堆转储(崩溃 JVM)的方法,我如何使 JVM 崩溃以获取这些日志?

You are confusing an exception or error being thrown as a JVM crash.您正在混淆作为 JVM 崩溃抛出的异常或错误。

A JVM crash occurs due to an internal error in the JVM, you cannot trigger this by writing a normal Java program (or should not unless you find a bug) JVM 崩溃是由于 JVM 中的内部错误而导致的,您无法通过编写普通的 Java 程序来触发此崩溃(或者除非您发现错误,否则不应触发)

What you are doing is triggering an Error which means the program continues to run until all the non daemon threads exit.您正在做的是触发一个错误,这意味着程序将继续运行,直到所有非守护线程退出。

The simplest tool to examine the heap is VisualVM which comes with the JDK.检查堆的最简单工具是 JDK 附带的 VisualVM。 If you want to trigger a heap dump on an OutOfMemoryError you can use -XX:+HeapDumpOnOutOfMemoryError如果你想在 OutOfMemoryError 上触发堆转储,你可以使用-XX:+HeapDumpOnOutOfMemoryError

Use Jmap使用Jmap

jmap [options] pid

pid is the process id of application pid 是应用程序的进程 ID

When you see the below当你看到下面

Exception in thread "main" java.lang.OutOfMemoryError

It means your error or exception is handled by the exception handler.这意味着您的错误或异常由异常处理程序处理。 This is not a crash.这不是崩溃。

Eclipse has an awesome Heap Analyzer Eclipse 有一个很棒的堆分析器

Also, you can use jps to get the PID, and then jmap for the heap itself.此外,您可以使用jps获取 PID,然后使用jmap获取堆本身。

In case, you want to crash the JVM, your best guess would be native code.如果您想让 JVM 崩溃,您最好的猜测是本机代码。

Find the process id for which you want to take the heap dump查找要为其进行堆转储的进程 ID

ps -ef | grep java

Once you get PID by running above command run below command to generate heap dump.通过运行上面的命令获得 PID 后,运行下面的命令来生成堆转储。

jmap -dump:format=b,file=<fileName> <java PID>

You can pass below JVM arguments to your application:您可以将以下 JVM 参数传递给您的应用程序:

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=

This argument will automatically trigger heap dump in the specified 'file-path' when your application experiences OutOfMemoryError.当您的应用程序遇到 OutOfMemoryError 时,此参数将自动触发指定“文件路径”中的堆转储。 There are 7 different options to take heap dumps from your application:有 7 种不同的选项可以从您的应用程序中获取堆转储:

  1. jmap地图
  2. -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError
  3. jcmd jcmd
  4. JVisualVM虚拟机
  5. JMX JMX
  6. Programmatic Approach程序化方法
  7. Administrative consoles管理控制台

Details about each option can be found in this article .可以在本文中找到有关每个选项的详细信息。 Once you have captured heap dump, you may use tools like Eclipse Memory Analysis tool , HeapHero to analyze the captured heap dumps.捕获堆转储后,您可以使用Eclipse 内存分析工具HeapHero工具来分析捕获的堆转储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM