Comparing Heap Dumps with NetBeans 7.1
One of the "new and noteworthy" features of NetBeans 7.1 is the ability to compare two heap dumps. Specifically, the easy-to-use feature allows one to quickly ascertain the difference in number of each class's instances between two dump files. This brief post shows how easy it is to use.
Heap dumps can be generated in a variety of ways. For purposes of this post, I have created two heap dumps on two totally separate applications using the Oracle JDK-provided jmap (Java Memory Map) tool to attach to the processes's Java PIDs and create heap dumps (creatively named Heap1.map and Heap2.map in my example). It's probably not useful in most cases to compare heap dumps of two entirely different applications. I only do so in this example to make the differences calculated in the comparison results obvious.
The next screen snapshot shows NetBeans 7.1 started with the option "Load heap dump..." being selected under the "Profile" menu. This is done to load the first heap dump of the two to be compared.
The next screen image shows the results of selecting "Load heap dump..." (the popup titled "Open Heap Dump File") and indicates selecting one of the heap dumps (Heap1.map).
The next screen snapshot shows how NetBeans 7.1 looks with the selected heap dump displayed with the "Summary" tab in focus.
The last image showed the heap file loaded in NetBeans 7.1 with the "Summary" tab in focus. The next image is a snapshot of the IDE switched to the "Classes" tab in the heap dump report area.
The last image indicates that NetBeans shows the number of instances of different classes in the Java application whose heap dump is being analyzed. By default, the instances are listed in descending order of number of instances, but one can click on the "Size" column header to sort by total size. Just above the column headings "Instances" and "Size" is a link that says, "Compare with another heap dump" and that is the main focus of this post.
Clicking on the "Compare with another heap dump" link in the "Classes" tab of NetBeans 7.1's heap dump analysis tool leads to a popup ("Select Heap Dump to Compare") for selecting the second heap dump to be compared. This is indicated in the next screen snapshot.
In my example, I had two other heap dump files in the same directory as the originally selected one (Heap2.map and Heap3.map). Selecting Heap3.map leads to the comparison being made. This can take a few seconds for the NetBeans IDE to update the representation and it might be worth moving the scroll bar on the right down and back up to make sure it has updated. The next two screen snapshots show the comparison displayed with the first screen snapshot showing the types for which the first heap dump has more instances than the other (top) and the second image showing the types for which the second heap dump has more instances than the other (bottom).
The + and - signs indicate the number of instances greater (+) or fewer (-) in the first heap dump file analyzed (Heap1.map) as compared to that of the second heap dump file analyzed (Heap3.map). In other words, types with positive numbers have more instances represented in the originally loaded heap dump and types wiuth negative numbers have fewer instances represented in the originally loaded heap dump.
When comparing two separate applications, this doesn't provide much that's interesting other than to illustrate the tool. However, given two instances of the same application at the same execution point with different context or data sets might prove more interesting when comparing heap dumps. Perhaps the most enlightening use of this tool is for comparing heap dumps taken from the same application at different points in its execution. With NetBeans 7.1, it is easy to rapidly determine the difference between two heap dumps in terms of instances loaded at the time each heap dump was taken. When it's the delta or difference that matters, this tool can be useful.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)