3

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

4

4 回答 4

7

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

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

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

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

我想你可以使用kill -3而不是jstack.

于 2012-07-26T12:39:13.313 回答
5

你可以使用这样的代码从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.");

}

然后像这样安排任务

 final ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(
            new Runnable() {dumpActiveThreads()},
            0,
            30, TimeUnit.SECONDS);
于 2012-07-26T12:39:13.780 回答
1

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

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

${JAVA_HOME}/bin/jmap -dump:file=/home/MyDump.hprof PID
于 2012-07-26T12:43:18.987 回答
0

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

于 2016-03-16T19:08:36.233 回答