【发布时间】:2012-07-25 01:07:25
【问题描述】:
请帮忙,我想运行一个shell脚本,它应该使用kill -3命令每30秒进行一次jave堆转储。提前致谢。
【问题讨论】:
-
你看过cron吗?
-
@Thomas 使用
cron每 30 秒触发一次? -
没错,cron 只能降到每 60 秒一次。
请帮忙,我想运行一个shell脚本,它应该使用kill -3命令每30秒进行一次jave堆转储。提前致谢。
【问题讨论】:
cron 每 30 秒触发一次?
你试过这么简单的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。
【讨论】:
kill -3,但我认为看不到堆栈转储是我的错。
您可以使用这样的代码从 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);
【讨论】:
我没用过kill -3命令,但是我用过sun sdk提供的jmap命令
您可以编写一个脚本,然后在脚本中运行以下命令。
${JAVA_HOME}/bin/jmap -dump:file=/home/MyDump.hprof PID
【讨论】:
3 将只提供线程转储而不提供堆转储。线程转储意味着您只能检查 JVM 中每个线程的堆栈跟踪。但是您正在寻找 linux 上的堆转储然后需要使用以下命令。 jmap -dump:file=myheap.bin {您要进行堆转储的pid}。输出“myheap.bin”不是人类可读的,要读取文件,您可以使用 MAT 工具。 MAT下载链接:http://www.eclipse.org/mat/
【讨论】: