0

I want to generate the thread dump of my running application. However, when I do kill -3 PID, it generate the thread dump at the console of the running application.

I want to generate it to a file. I know I can run the application as ./run.sh > thread_dump or I can also use jstack utility to redirect it to file.

However, I do not want to use any of those. In another approach I redirected the complete output of my console to a file by:

            File file = new File("out.txt");
    FileOutputStream fos = new FileOutputStream(file);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);

Now every output is written in out.txt. But still, when I do kill -3 , it writes the thread dump to console.

What am I missing in the third approach? Would anybody know, what class/source is called when kill -3 is called on a JVM, so that I can look how thread dump is exactly written on making a kill -3 request/command?

Archit Thakur
  • 105
  • 1
  • 1
  • 8
  • The source for this is entirely in the JVM in C++. You need to have a build of the OpenJDK, which you can modify to dump to a file instead. BTW YOu can write your own thread dump using JMX and Thread.getAllStackTraces() etc but this won't be triggered by `kill -3` – Peter Lawrey Jul 29 '13 at 09:02
  • Then, How does tomcat writes thread dump to a file by default. – Archit Thakur Jul 29 '13 at 09:25
  • I mean how does tomcat writes thread dump to catalina.out file. – Archit Thakur Jul 29 '13 at 09:36
  • Looking at catalina.sh, it runs java with output redirected to catalina.out. They use the solution you mention in your second paragraph. – John M Jul 30 '13 at 17:38

1 Answers1

0

You need to use the jmap -dump option to dump heap dump of a running application. Here is an interesting blog on the oracle website describing how to Programmatically dump heap from Java applications.

https://blogs.oracle.com/sundararajan/entry/programmatically_dumping_heap_from_java

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136