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.
-
Have you looked into [cron](http://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds)? – Thomas Jul 26 '12 at 12:39
-
@Thomas Use `cron` to trigger every 30 seconds? – Peter Lawrey Jul 26 '12 at 13:00
-
True, cron can only get down to every 60 seconds. – Thomas Jul 27 '12 at 02:24
4 Answers
Have you tried such a simple shell script?
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
.

- 334,321
- 69
- 703
- 674
-
1+1 but kill -3 won't dump the stack to the console that you run it from, unlike jstack – Brian Agnew Jul 26 '12 at 12:41
-
@BrianAgnew: thanks, I tried first with `kill -3` but I thought it's my fault that I can't see the stack dump. – Tomasz Nurkiewicz Jul 26 '12 at 12:43
-
kill -3 will dump it to the process' stdout (err?) but jstack appears to dump it to the jstack's console – Brian Agnew Jul 26 '12 at 12:49
you can do thread dumping from java application using code like this
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);

- 3,160
- 18
- 18
I have not used kill -3
command but i have used jmap
command provided by sun sdk
You can write a script and then in script run below command.
${JAVA_HOME}/bin/jmap -dump:file=/home/MyDump.hprof PID

- 7,831
- 3
- 35
- 54
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/

- 425
- 1
- 5
- 14