Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug a hanging java thread?

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?

like image 528
Jim Avatar asked Jul 10 '12 05:07

Jim


People also ask

How do you troubleshoot a hung Java process?

If the application appears to be hung and the process appears to be idle, then the first step is to try to get a thread dump. If the application console is available, then press Control+\ (on Oracle Solaris or Linux), or Control+Break (on Windows) to cause the HotSpot VM to print a thread dump.

How does Java handle hung threads?

The Java ThreadPool does not have a mechanism for detecting hanging threads. Using a strategy like fixed threadpool ( Executors. newFixedThreadPool() ) will not work because if some tasks hang over time, all of the threads will eventually be in a hung state.

How do I run Java in debug mode?

A Java program can be debugged simply by right clicking on the Java editor class file from Package explorer. Select Debug As → Java Application or use the shortcut Alt + Shift + D, J instead. Either actions mentioned above creates a new Debug Launch Configuration and uses it to start the Java application.


2 Answers

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

like image 92
aviad Avatar answered Oct 20 '22 10:10

aviad


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...

like image 40
Mark Bramnik Avatar answered Oct 20 '22 09:10

Mark Bramnik