java 如何使用 kill -3 <pid> 命令每 30 秒进行一次 jave 堆转储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11669419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:58:41  来源:igfitidea点击:

how to take jave heap dump every 30 seconds using kill -3 <pid> command

javashellheapdump

提问by Raghava Malluvaiah

Please help in this, i want to run a shell script where it should take jave heap dump every 30 seconds using kill -3 command. Thanks in advance.

请帮助解决这个问题,我想运行一个 shell 脚本,它应该使用 kill -3 命令每 30 秒进行一次 jave 堆转储。提前致谢。

回答by Tomasz Nurkiewicz

Have you tried such a simple shell script?

你试过这么简单的shell脚本吗?

while true
do
  jmap -dump:file=/tmp/java-`date +%s`.hprof PID_OF_JVM
  sleep 30
done

This will create one file pear each snapshot. For thread dump you can use similar script:

这将为每个快照创建一个文件。对于线程转储,您可以使用类似的脚本:

while true
do
  jstack PID_OF_JVM > stack-`date +%s`.txt
  sleep 30
done

I guess you can use kill -3instead of jstack.

我想你可以使用kill -3代替jstack.

回答by Andrey Borisov

you can do thread dumping from java application using code like this

您可以使用这样的代码从 Java 应用程序进行线程转储

 public static String getDumpFor(Thread thread) {
    StringBuilder sb = new StringBuilder();
    if (thread.isAlive()) {
        StackTraceElement[] stackTrace = thread.getStackTrace();
        sb.append(thread.toString()).append("\n")
                .append(String.format(" State - %s,", thread.getState()))
                .append(String.format(" Is daemon = %s,", thread.isDaemon()));
        for (StackTraceElement s : stackTrace)
            sb.append("\tat ").append(s.getClassName()).append(".").append(s.getMethodName()).append("(").append(s.getFileName()).append(":").append(s.getLineNumber()).append(")")
                    .append("\n");
    }
    return sb.toString();
}


public static void dumpActiveThreads() {
    Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
    Set<Thread> keySet = stackTraces.keySet();
    System.out.println("\nThread dump begin:");
    for (Thread thread : keySet)
        dumpActiveThread(thread);
    System.out.println("\nThread dump end.");

}

and then schedule task like this

然后像这样安排任务

 final ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(
            new Runnable() {dumpActiveThreads()},
            0,
            30, TimeUnit.SECONDS);

回答by amicngh

I have not used kill -3command but i have used jmapcommand provided by sun sdk

我没有使用kill -3命令,但我使用jmap了由提供的命令sun sdk

You can write a script and then in script run below command.

您可以编写一个脚本,然后在脚本中运行下面的命令。

${JAVA_HOME}/bin/jmap -dump:file=/home/MyDump.hprof PID

回答by Panky031

3 will give only the thread dump but not the heap dump.Thread dump means you can only check the stack traces for each thread in the JVM.But you are lookin g for the heap dump on linux then need to use the below commands. jmap -dump:file=myheap.bin {pid of which you are looking to take heap dump}. The output "myheap.bin" is not human readable,to read the file you can use the MAT tool. MAT download link: http://www.eclipse.org/mat/

3 将只提供线程转储而不是堆转储。线程转储意味着您只能检查 JVM 中每个线程的堆栈跟踪。但是您正在 linux 上寻找堆转储,然后需要使用以下命令。jmap -dump:file=myheap.bin {您要获取堆转储的pid}。输出“myheap.bin”不是人类可读的,要读取文件,您可以使用 MAT 工具。MAT下载链接:http: //www.eclipse.org/mat/