Sunday, September 14, 2014

Java built-in profiling and monitoring tools

Java profiling is very useful technique to find performance bottlenecks and/or to solve complete system failures. Common bug that can occur in any system size in Java are slow service, JVM crashes, hangs, deadlocks, frequent JVM pauses, sudden or persistent high CPU usage or even the dreaded OutOfMemoryError (OOME)

Finding this kind of bugs is like art and you need lot of experience to be good at it. That's why some of programmers specialize in Java profiling. In some cases fining bug is impossible if you don't know how system works. Every Java programmer should know at least what are basic profiling tools, because you can't always pay some external specialist to fix memory leaks or deadlocks for you.

Java comes with built-in tools for profiling and monitoring. Some of these tools are:

jmap

This is internal Java tool and it is not profiling tool as such, but it is very useful. Oracle describes jmap as an application that “prints shared object memory maps or heap memory details of a given process or core file or remote debug server”. And it is exactly that. Most useful option is to print memory histogram report. The resulting report shows us a row for each class type currently on the heap, with their count of allocated instances and total bytes consumed. Using this report you can easily identify memory leaks if you have any.

jstack

JStack is also not profiling tool, but it can help you identify thread deadlocks. The output of "jstack" is very useful for debugging. It shows how many deadlocks exist in this JVM process and stack traces of waiting threads with source code line numbers, if source codes were compile with debug options.

jconsole


JConsole is a graphical monitoring tool to monitor Java Virtual Machine (JVM) and Java applications both on a local or remote machine.  It is using for monitoring and not profiling, so you are better with using VisualVM described bellow.

VisualVM

Another tool currently built into the JVM is VisualVM, described by its creators as “a visual tool integrating several command line JDK tools and lightweight profiling capabilities”. This tool can generate memory graph that will show you how your application is consuming memory through time. VisualVM also provides a sampler and a lightweight profiler. Sampler lets you sample your application periodically for CPU and Memory usage. It’s possible to get statistics similar to those available through jmap, with the additional capability to sample your method calls’ CPU usage. The VisualVM Profiler will give you the same information as the sampler, but rather than sampling your application for information at regular intervals.

For me these built-in tools work quite well, but if you want more specialized and more powerful tools for profiling  you can check: BTrace, EurekaJ and Eclipse Memory Analyzer (MAT).




No comments:

Post a Comment