Saturday, March 20, 2010

Automatically dump your Thread´s StackTrace whenever something get wrong

JVM either from SUN or IBM has features that allows us to do heap dump and thread stack trace dump. They can even do it automatically when there is OutOfMemory error.
But there is plenty of others situations that StackTrace of Threads can be useful e.g. when your application code enter into infinite loop or it forgets to free some resources or all threads of its thread pools are busy. In such cases Thread´s StackTrace are extremely valuable in root cause analysis.
Create StackTrace dump is also much cheaper and faster than Heap dump so that is why we should do it automatically in many situation when we suspect that something gets wrong.
The most easy way to do it to write a script that trigger a Thread´s StackTrace dump by invoking kill -3 jvm_pid (or jstack jvm_pid) when something happen( e.g. JVM use more then 90% CPU).