views:

350

answers:

5

I need to determine which locks are the most-contended-for in my application code. What free tools can I use to determine this ?

+2  A: 

VisualVM (Part of Java 1.6) (see)

Kai
Doesn't VisualVM require a separate plugin for that?
kd304
+2  A: 

you can use jconsole or jstack both in the bin directory of your JDK. The jconsole in particular allows you to connect to your process and monitor the threads including which resources they have locked, and it can help you identify a deadlock state.

pfranza
I think the question relates to some statistics about the lock contention during the application run - which are the hot locks in the system.
kd304
+1  A: 

The JDK has some built-in support - under unix, kill -3 the process, under windows, ctrl-break. This will display a complete thread dump, followed by any deadlocks detected. Plus, in the thread dusmp you can see what threads own what locks, and compare them to each other.

Don Branson
+1  A: 

You can also view this in eclipse's debugger. In the Debug view, use the little down-triangle menu on the view toolbar to turn on "Java->Show Monitors".

When you suspect a deadlock, pause the application (select the application in the debug view and press the pause button on the debug view toolbar) and if there's a deadlock, the blocking threads will turn red. If you expand those threads you can see the lock contention.

Scott Stanchfield
+1  A: 

If you own the code, you could create/look for a Lock implementation which gathers contention statistics. If not, try the tools suggested in the other posts.

kd304