Sunday, 9 September 2012

Under the hood with VisualVM

Few people realise that an excellent lightweight profiler comes with the Oracle JDK.

VisualVM specializes in JVM monitoring and performance analysis.

Most people should have it installed with the JDK, on my machine its in :-
C:\Program Files\Java\jdk1.7.0_05\bin\jvisualvm.exe

Otherwise if you can't find it simply go and download it here, unzip the archive to a suitable place.

You can then run VisualVM using \bin\jvisualvm.exe or \bin\visualvm.exe


Goto Tools\Plugins and first install the default plugins :-


VisualVM Extensions
Visual GC
Threads Inspector
Tracer-Swing Probes
Tracer-JVM Probes
Tracer-Collections Probes
Tracer-Jvmstat Probes
Tracer-Monitor Probes

For concurrency analysis you can get additional ThreadDumpAnalyzer plugins here.

Logfile Plugin Version 2.2
TDA Library Plugin Version 2.2
TDA VisualVM Plugin Version 2.2
TDA 2.2 Final - Optional

Use Tools\Plugins\Downloaded to install the custom plugins (*.nbm files).

After installing the plugins then restart VisualVM.

Overview

After starting VisualVM you see a list of running JVM processes on the left. Double clicking a process will open the main tabbed interface. The tabs allow you to investigate different aspects of your programs runtime behaviour, like memory usage, running threads, GC generations, etc.


Heap Dump

You can generate a Java heap dump by using a right click menu on the process or from a button on the Monitor tab.


You can then analyse the heap dump using VisualVM.
You can see the objects on the heap, and how much memory they use. You can find the root reference that is causing the instance to remain alive by looking in the references window.

Garbage Collection

You can observe the garbage collector and the size of the generations using the VisualGC plugin.


Profiler

The Profiler and Sampler tabs give basic profiling functionality.
The Profiler allows code to be instrumented and then uses this to measure how much time is spent in each method.
The ‘Sampler’ looks at the execution stack at a certain sample rate and from there infer time spent / memory used by function.



Threads Inspector

You can observe running threads in the threads inspector, these include both JVM threads like the finalizer thread as well as your own application threads.


Tracer Plugins

The ‘Tracer’-plugins allows monitoring of specific things in the VM, mostly stuff that is exposed using JMX, things like IO usage and JIT compilation.

Conclusion

Sure there is the excellent free jmap for statistics and heap dumps useful for triage in production, there is the Eclipse open source TPTP, then there are commerical tools like JProfiler and YourKit.
However free is hard to beat, and VisualVM is a useful tool to have in anyones toolbox! 

No comments:

Post a Comment