Top Tags

Capture JVM thread dump automatically

In one of my recent projects, I was faced with an issue in a WebLogic 10.3.5 domain running on JRockit, where the JVM was sporadically reporting a flood of threads being spawned. When I say flood it means exactly 16383 threads, always!

Having these errors was one issue which I will deal with in another post. Identifying what was creating these was another. Easiest and most accurate way was to take a thread dump. Challenge was, when! Notice that these error messages do not report any time or date stamp. So, I created a UNIX shell script which did following:

Ran in the background polling the JVM for the count of live threads

As soon as the number of live threads exceeded a configured threshold, took a thread dump

Waited for a configured interval and then took another thread dump

The total number of thread dumps did not exceed a predetermined and configured value

The script was unaffected even if the JVM was restarted

Here is the script that I used. Note that it may or may not work exactly as intended as a lot depends on the shell environment. Hence, feel free to tweak it according to your requirement.

#!/bin/bash

####################################################### Overview: This script captures thread dump of a JRockit# JVM in case the number of live threads exceeds a# certain threshold. This can help in diagnosing# which code possibly creates those number of threads.######################################################