简体   繁体   English

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

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

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. 请帮忙,我想运行一个shell脚本,它应该使用kill -3命令每30秒进行一次jave堆转储。 Thanks in advance. 提前致谢。

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 -3 instead of jstack . 我想你可以使用kill -3而不是jstack

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);

I have not used kill -3 command but i have used jmap command provided by sun sdk 我没有使用kill -3命令,但我使用了sun sdk提供的jmap命令

You can write a script and then in script run below command. 您可以编写脚本然后在脚本运行下面的命令。

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

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. 3只会给出线程转储而不是堆dump.Thread转储意味着你只能检查JVM中每个线程的堆栈跟踪。但是你在linux上查看堆转储然后需要使用下面的命令。 jmap -dump:file=myheap.bin {pid of which you are looking to take heap dump}. jmap -dump:file = myheap.bin {pid,你正在寻找堆转储}。 The output "myheap.bin" is not human readable,to read the file you can use the MAT tool. 输出“myheap.bin”不是人类可读的,要读取文件就可以使用MAT工具。 MAT download link: http://www.eclipse.org/mat/ MAT下载链接: http//www.eclipse.org/mat/

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

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