The Beep Breakpoint
Sunday, January 28th, 2007Sometimes you would like to know how often a method is called. You don’t need to know the exact number but just have a feeling of how often it is.
The traditional technique would be to add a System.out.println() but that means that you have change your source file, recompile, play with your application while keeping an eye on the console and then remove your trace code. If the code you want to watch is in a library or in the JDK, then it becomes much harder.
There is an easier way…
I remember an article in Dr Dobb’s journal that described how to analyze the performance of a program using an oscilloscope. The paper described how to emit a signal on the RS232 port on which you would connect the oscilloscope.
The technique described here is similar but don’t worry, you will not need an oscilloscope, we will just use a nice feature of JDeveloper’s debugger, the “Beep Breakpoint”
Imagine your Swing application displays a JTree and you would like to know how often the tree’s cell renderer is invoked.
- Go to the
DefaultTreeCellRendererclass and put a breakpoint in thegetTreeCellRendererComponent()method. JDeveloper may warn you about debugging being disabled in that class and offers you to change the setting. Click Yes and clear the two fields you see in the dialog. - Edit the breakpoint properties (right-click the breakpoint icon in the margin and select Edit…)
- Switch to the Actions tab and only leave the “Beep” box checked:
- Press the Debug button…
Now every time that line is executed you will hear a beep. If the line is executed a lot you will hear a lot of noise.
There is another interesting usage of that technique. You may want to verify what parts of your application exercise a piece of code. Finding the usages of the method is not always convenient because many paths can lead to the method. Using a simple breakpoint does not work because it would interrupt the application for usages you did expect.
Put a Beep breakpoint in the method you want analyze and test your application. You will hear a beep every time the method is executed and it will be very easy to detect what part(s) of the application unexpectedly calls the method.
The Beep Breakpoint is not the only advanced feature of JDeveloper’s debugger but it certainly is the most annoying for your coworkers.
