11

This is a more specific question to follow up on [another question that I have asked recently] 1. A correct answer for this question will earn a correct answer for that previous question too (since that is still in limbo)!

Basically I have a Java desktop application with a memory leak issue. I am using the memory profiler in NetBeans IDE to profile the memory issue. These are the steps that I have taken done so far:

  1. Attach a new memory profiler to the NetBeans project
  2. Define profiling points at several careful chosen lines of code, and set them to trigger a memory heap dump
  3. Run the application in profiling mode

The end result of this is that I have several memory dumps saved to disk in *.hprof files. NetBeans IDE lets me peruse the contents (basic sort and search) of these memory dumps, and even lets me walk the heap, by seeing what the references contained within each instance, and what other objects reference each instance. That is all good, and I have been able to identify 1 or 2 fairly obvious memory leaks and rectify about 15% of the problem thus far.

However, right now the method I am using relies on creating hypotheses about which objects should NOT be in memory at a particular point of time, and then investigating those. What I am after now is a way to compare two separate heap dumps: Basically I have two heap dumps that should be almost the same, because the application has been restored to the same state.

However, one is before the memory leak, and the other after the memory leak, and so they are obviously different. If I am able to compare these two heaps using a of tool, instead of manually as I am doing now, then I need not rely on hypotheses to identify where the leaks are occurring, and can just have the tool identify them for me.

This is important for me because of the the sheer number of classes and instances involved for this particular application (700+ and millions, respectively)

Is the NetBeans IDE's profiler capable of doing this?

If not, is there a tool out there that is able to perform this task?

3 Answers 3

31

There is also a free, GUI tool for this task: VisualVM. It lets you take several heap dumps and then tell it to compare one with another, displaying the differing contents as a list, with a graphical representation of each element's share of used memory. Also, interactively browsing the heap dump difference is much more comfortable than with jhat.

VisualVM 1.4.4 compare capability

10
  • 2
    It would be nice if the person who downvoted my answer explained to me why she/he did it. Thank you. Commented Apr 9, 2013 at 11:45
  • 2
    I have taken a couple heap dumps with VisualVM; now how do I compare them?
    – 11101101b
    Commented Sep 6, 2013 at 21:50
  • 1
    still not answered and it is certainly not obvious in the UI.
    – Ray Kiddy
    Commented Dec 2, 2016 at 22:25
  • 1
    found it. in the Summary tab, upper right, there is a "compre two heap dumps" link. Less filling, tastes great.
    – Ray Kiddy
    Commented Dec 2, 2016 at 22:33
  • 2
    It's been moved to the top-right corner of "classes" inside the heap dump screen. Commented Jul 29, 2018 at 22:27
16

You could use jhat. Specifically look at the option(-baseline baseline-dump-file) on the page I reference it says the following:

"Specify a baseline heap dump. Objects in both heap dumps with the same object ID will be marked as not being "new". Other objects will be marked as "new". This is useful while comparing two different heap dumps."

this may help when comparing the two heap dumps.

2
  • That requires both heap dumps to be from the same run of the application (which doesn't seem like it'd be a problem from bguiz). Commented Nov 21, 2009 at 3:52
  • @broschb, +1, Thank you - this works for me! My only issue with JHAT is that it's interface is via a fairly static webpage, not very user friendly. That being said being able to walk the heap dump using References to this object, and being able to filter those with New Instances/Include subclasses, was exactly what I wanted!
    – bguiz
    Commented Nov 23, 2009 at 0:41
4

YourKit can compare heap dumps.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.