Thursday, October 15, 2009

kill -3 to the rescue!

A basic and ancient weapon to keep close at hand when hunting down deadlocks and other weird threading issues in Java applications is the kill -3 command. I don't know of an equivalent on Windows, but if you are running on any flavour of Unix (including Mac OS X) it is available via the command line.

If you've never tried it, basically it gives you a dump of the stack traces of all living threads in the process. Despite the name, when you use the -3 option, it doesn't kill the process. This is handy if you want to take multiple snapshots a few seconds apart to see if anything changes.

Even better, it also shows you what objects have been locked, where, by which threads; and which objects are waiting for those locks. In no time at all you can find the source of the deadlock and do something about it.

I've only used this in development, but there's no reason you couldn't use it in production, provided your stdout and stderr are redirected somewhere you can see. Since it doesn't kill the process, it's entirely plausible you could find a workaround for some problems on the running system.

No comments:

Post a Comment