memory profiling with jvisualvm

I’ve been meaning to blog about jmap/jvisualvm for a while, but this JavaRanch question finally prompted me to do so.

My goal was to see where the memory was going in our JForum installation at JavaRanch. This was a one time thing, so I decided to see if I could do so without JProbe.   A colleague had already used Eclipse’s Memory Analyzer.  (Awesome tool by the way – it clues you right in to what it suspects are leaks.)  I decided to use jvisualvm.  Which was interesting.  While I was off exploring, someone else solved the problem and I put writing this up on the side.

I tried this locally on my machine to make sure I knew what to do before trying it on the production UNIX/Linux box.

How to run jvisualvm

  1. Get your process id:
    • Windows: JPS can be used to find your process id on windows.  At the DOS prompt, type “jps”.  My Tomcat process is called “bootstrap”  [I figured this out by stopping the process, running jps and then restarting the process running jps again.  I also had an unnamed process (Eclipse maybe?) and for jps itself.]  The JavaDoc says jps may not be supported in the future, but it works right now on Windows XP.
    • UNIX: You can simply use ps -ef | grep <whatever makes your process name identifiable>.  In my case, it was “ps -ef | grep coderanch”
  2. Run “jmap -dump:file=heap.bin.hprof 1234” [if your process was 1234].  Note this requires Java 6.
  3. Run “jvisualvm”
  4. file –> load –> choose hprof file from jmap
  5. Filter by classname – I used the package name net.jforum to narrow things down.  Which turned out to be a mistake as the memory leak was in Lucene.

or – Run” jhat -J-mx512m heap.bin.hprof”.  Thanks to this blog for helping with the jmap/jhat command.

[edited to fix typos]