Java Profiling with MonkeyWrench
Garbage Collection Profiling
While most Java profilers provide a view on garbage‐collection activity, the MonkeyWrench provides a slightly different perspective than most. This view, found on the Garbage Collection screen and shown in Figure 9, provides two time series: one of the number of garbage collections for each display interval, and one of the CPU time spent in garbage collection during the interval.
Most profilers provide a plot of the amount of memory used in a generation as a function of time. While this view can be useful when garbage collection does not occur frequently, it breaks down in situations where (for example), the Eden generation is being collected multiple times per second (which is not an unusual situation). In these cases, the sampling interval of the profiler is too large to capture the fact that many collections are occurring during each sampling interval, and a plot of the generation memory as a function of time can appear to be a random number. Not being aware of this situation can cost a lot of troubleshooting time.
When MonkeyWrench first begins monitoring garbage collection, the cursor for the current time (represented with a t = Now cursor in red) progresses from the left to the right edge of the screen. Time is marked in time before the present time (that is, the cursor), displaying the sampling interval chosen on the Configuration screen. When the “t = Now” cursor reaches the right edge of the graph, the x axis remains fixed and the data points scroll to the left. A combo box at the top of the screen allows you to switch between garbage collectors.
Undergoing multiple garbage collections per second is not necessarily undesirable; more important is the amount of time spent in garbage collection during an interval. In the example shown in Figure 9, at peak, about 40 minor collections are occurring per second, burning about 50 msec of CPU time. If an application is experiencing a very large number of collections (minor or otherwise) in an interval, and using a lot of CPU time to collect, it might signal that the generation is sized too small, or it may indicate that there is an issue in the target application requiring attention.
Figure 9 -- Garbage Collection Screen
For example, when profiling a Swing application, if you notice that a lot of minor collections occur during certain mouse movements, or when selecting a menu item, it may point to an over‐engineered AWT event listener. While the collection display won’t actually point to the section of code at fault, knowing something about what is happening when the collection activity spikes will generally help narrow down the list of possible causes.
As with the other MonkeyWrench screens, two “clear” buttons are provided: one to clear garbage‐collection profiling data, and another to clear all profiling data. This feature allows you to clear the display before triggering a target application event of interest, so that the display will show only activity since the event of interest occurred.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)