kill -3 is your friend

One nice feature of Java runtime is when you send the QUIT signal to a Java process, it outputs the full thread dump to stdout. To send the that signal, just open a terminal and type:

kill -QUIT <pid>

or

kill -3 <pid>

Where <pid> is the process Id. This does not terminate the process; all threads will continue doing what they were doing.

That feature can be very useful when the application seems to freeze or when you have a very intermittent issue (intermittent deadlock). With the full thread dump, you can see what every thread was doing at that particular moment. So in case of a deadlock, you will be able to see what monitors and what threads are involved.

This can also be helpful to diagnose performance bottlenecks. Suppose you are load testing an application and it does not deliver the expected throughput, but the CPU usage is not the problem. For instance, with kill -3 you will notice right away that the size of the jdbc connection pool is not big enough and all threads are waiting on it for a connection to free.

Remote Debugging in Java

One thing people ask me from time to time is how to debug a remote Java application. This can be very useful when you experience problems at customer site, but cannot reproduce them in a development environment. We all know logs files do not always contain all information required to solve the issues. In such case, remote debugging can be very useful.

To start remote debugging, one simply needs to add extra VM arguments to the Java command line:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

Then, you can use the remote feature of your IDE to connect to the Java process in using the port specified above (8000). For Instance, in eclipse, you would do:

Run -> Debug Configurations… -> Remote Java Applications -> Create new

In the Host field, you enter the host name or IP of the machine running the Java application. In the Port field, you enter the port specified above (8000).

Note that remote debugging also works nicely with SSH tunnels.