In the last couple of weeks, I had to do lots of profiling of our server code. Normally I use YourKit, easy to setup, and it has all the tools I need to do profiling such as CPU and Heap display graphs, force GC option, display of objects with their instance counts and sizes and so on. This time though I had to do profiling when server was under load and YourKit did not behave as I expected. I could see the memory usage in the main graph but when I took a snapshot of the heap, the snapshot took a while, and it didn’t match with the graph I was observing. I’m guessing that since the server is under load, by the time YourKit could take the snapshot, the data was not relevant anymore. This didn’t help me of course, so I turned to other tools.
Luckily, a coworker recommend Sun’s free VisualVM. I think it comes with JDK on Windows, but not on Mac OS, so I had to download it. I was pleasantly surprised with the VisualVM. First, I didn’t have to do anything special to use it, no JVM options or anything like that to make it detect my running Tomcat, it was just displayed under applications:
The main view included CPU, memory, classes and threads graph as you would expect from a profiler.
The Heap and Thread dump is as easy as right clicking on the application and it was fast and seemed accurate.
And finally, the Heap Dump showed me the classes taking up the memory, just what I needed…
One thing that was missing in VisualVM was the “GC roots” option in YourKit. I also did not see much Thread blocking information displayed but I didn’t spend too much time with VisualVM, maybe it’s there. Overall, I’m glad that there’s another free tool out there for Java profiling.