I have a Java process. I am using log4j for logging purpose. I have specified in log4j.xml, location and name of log file. logging is working fine. I have problem with kill -3 logs here.
I trying to get process-dump using kill -QUIT <pid>/ killl -3 <pid>. I expected the dump generated by kill -3 to get updated in log file specified in log4j.xml.
But it is not happening that way. I need the dump to observer thread statuses. I do not know any other way to get process dump of a running process.
kill -3 should output to stdout, so it should be wherever your stdout goes. If not you could try playing with -XX:LogFile JVM option.
Alternative way is jstack as suggested by NPE.
Another alternative is to use jvisualvm - it will produce stackdump in its nice GUI and you can copy it from there.
I need the dump to observer thread statuses. I do not know any other way to get process dump of a running process.
You can use jstack for this.
Related
Hadoop jvm process hangs without any error message,
I want to take a look into what JVM processes are doing (where they are stuck).
When I program in C++, I used GDB that can be attached to a running process and show the call stack of the threads.
How can I do the same thing for JVM?
You may use following command
kill -3 [PID]
This will print stack traces of all threads to the console of your java process. Another option is to use jstack utility which is bundled with jdk. Jstack does the same thing.
If it doesn't help then profilers should help. They can gather a lot more data than one thread dump.
I would like to automatically create a thread dump as part of a log collection script I have written.
I know that it is possible to generate a thread dump using jstack or kill -3 . The customer running the log collection script will only have a JRE installed so jstack is not an option.
If I use jstack and the pid of the JVM that the install4j launcher creates I get the thread dump...however invoking kill -3 with the same pid generates no output. I am currently directing both stderror and stdout to files using the install4j launcher configuration.
This is a linux launcher that is configured with the service option.
FWIW...I have tried not directing the output and also running my application as a console program instead of a service and none of these allow 'kill -3' to work.
I'm using install4j 6.0
Lastly, I do not want to use -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=dump.log because of the unknown overhead it might create.
Any help would be appreciated!
I would suggest to use the ThreadMXBean to get this information programmatically in the same process rather than use signal handlers from another process. The API documentation is at
http://docs.oracle.com/javase/8/docs/api/java/lang/management/ThreadMXBean.html
Here's a full example of how to use this MBean.
In that way you can write the information explicitly to wherever you want it to go.
We have two logs in project, one is standard output using: java project.jar > output.log; the other is logback configured in the logback.groovy.
And when I use kill -3 to capture the thread dump info, they went to the output.log.
Any way to output these info to the logback ?
And if I add the param -verbose:gc, how to output to the logback as well?
Thanks:)
As far as I'm aware, you cannot capture this information within the Java application. In both cases, the output comes from the JVM native code libraries and doesn't go through the Java IO libraries.
I have the following problem:
I deploy a web application in Tomcat (Linux) and after shutdown of Tomcat, if I do ps -ef I still can see the java process running.
I believe this happens due to some hanging thread but I don't know how can I track this thread down.
How can I debug this issue?
You can generate 4-5 thread dumps as described below and then analyze them using tools like Samurai.
What you want to check is when a stuck thread or long running transaction happens, all the thread dumps will show a certain thread id is at the same line in your java stack trace. In simpler terms, the transaction is spanning across multiple thread dumps and hence needs more investigation.
Now when you run these through Samurai, it will highlight these in Red colour so you can quickly click on it and get to the lines showing issues.
See an example of this here. Look at the Samurai output image in that link. The Green cells are fine. Red and Grey cells need looking at.
Generating a Thread Dump:
(Linux)
If the JVM is running in a console then simply press Ctrl-\.
If the JVM is running in the background then send it the QUIT signal:
kill -QUIT process_id
There process_id is the process number of the running Java process. The thread dump will be sent to wherever standard out is redirected too.
You can generally get the process numbers of of all running Java processes with the command:
ps axf | grep java
You say your java process still exists, right?
Processes exist as long as they have attached threads, right?
If so, I would go for the following approach:
- run the process with the MBean server attached and managed internally by the JVM
Then connect to the process after you send the QUIT signal and get the thread dump (there should be a JMX for that. See which threads look suspicious to you.
I think you can also use JVisualVM to take thread dumps...
When I issue a kill -3 <pid> command to my Java program, it generates the thread dump on the console. How do I redirect this to a file?
Two options:
Run your Java application with stdout redirected
java com.example.MyApp > out.txt
Use jstack instead.
The jstack utility allows you to get a thread dump and send the output to the current console instead of the stdout of the Java application, allowing you to redirect it.
For example, if the PID of your Java application is 12345 (use the jps utility to find it quickly):
jstack 12345 > threads.txt
I usually use the NetBeans profiler, but jvisualvm is available from the command line.
If you want details of all threads and other JVM details, try jconsole.
Please append following JVM arguments to your application. Thread dump should be captured at dump.log.
-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=dump.log
Please note it does not redirect, but enables JVM diagnostic logging. So, there could be possible over head as well.
However, if you can have JDK in the environment, using jstack or jcmd (jcmd is preferred with JDK 1.8), you can capture thread dump and redirect to a file.
you can generate java thread dumps using 4 ways excluding the kill -QUIT way.