When your java application is getting unresponsive or requests are taking time, taking thread dumps will help find the cause.
If you take one thread dump, you might not be getting much information. You need to take at least 10 to 15 thread dumps with 3 to 5 seconds apart, and then you go through each thread dump to find what is causing threads to hang.
Getting thread dumps
Below is how you can take thread dumps with
- Find the JVM process id.
ps -ef | grep java
can be used to find the java pid. - Take thread dump using
jstack
for every 3 secondsWill generate ten thread dumps, taken 3 seconds apart.
Analyzing thread dumps
Now we have ten thread dumps first, open the first thread, and check for the request which is not responding.
For example, you know that the /atg/bcc
request is taking time, then search /atg/bcc
or respective session id
in the thread.
For example in below thread dump (td_1.log), where thread ajp-0.0.0.0-20109-24
with request uri /atg/bcc
is at socket read waiting for oracle database result.
Now we can check what is the state of this thread ajp-0.0.0.0-20109-24
in remaining threads.
Above will search for thread ajp-0.0.0.0-20109-24
and -A 2
will get the next two lines.
ls -ltr td* | awk '{print $9}'
will make sure that threads are listed in order.
Optionally use less -i
to open in less
to view and search for any additional patterns.
Below is how the sample output would be
As you can see, the above thread is waiting for the oracle result in the last thread dump, too (after 30 secs). We can check the main stack trace and see if the query is taking time due to a database issue. Similarly, you can also search for any lock acquired by threads.
You can also get snapshot of all thread dumps which are waiting on oracle result.
Output
First you search oracle.jdbc.driver.OracleResultSetImpl.close_or_fetch_from_next
(this can be any pattern you are searching for) and use -B 19
(print 19 lines before this pattern)
and on these results, search for ajp threads.
Conclusion
By following the above steps, you can analyze thread dumps to find issues with jstack
, grep
, ls
, ‘ awk’.
Hope this helps
– RC
Comments