Profile Your Applications with Java VisualVM
When you need to discover what part of an application is consuming more CPU or memory, you need to use a profiler. One profiler, packed by default with the Sun JDK is Java VisualVM. This profiler is really simple to use and really powerful.
In this post, we’ll see how to install it and use it to profile an application. Normally, to install it, you have nothing to do, because, it’s already installed with the JDK. But in several Unix systems, like Ubuntu, this is not the case. If you want to install it, just use apt-get (or aptitude) :
sudo apt-get install visualvm
To launch it just launch jvisualvm (jvisualvm.exe in the bin directory of the jdk for Windows). That will open the following window :
There is not a lot of interesting things to see here. To profile an application, you just have to launch it and VisualVM will detect it as launched :
After that, you just have to double click to view information about your running application. You’ve four tabs available for your applications (Overview, Monitor, Threads, Profiler). We’ll see all four tabs, first of all, the default tab, the overview :
This tab contains the main information about the launched application. You can see the main class, the arguments of the command line, the JVM arguments. You can also see what kind of JVM is running your program and where the JVM is located. And you can see all the properties set in the program.
A more interesting tab is the “Monitor” tab :
This tab follows the CPU and memory usage of your applications. You have 4 graphs in this view. The first one, from left to right, up to down, displays the CPU usage and the Garbage Collector CPU usage. The second graph displays the usage of the heap space and the PermGen space. The next graph displays the total number of classes loaded in the application and the last one displays the number of threads currently running. With these graphs, you can see if your application takes too muchCPU or if there is too much memory used by the application.
The third tab provides some details about Threads :
In this view, you can see how the different threads of the application are changing state and how they evolve. You can also see the time each thread passes in each state and you can have all the details about the threads that you want.
And now, I think the most interesting tab, is the Profiler one :
When you open this tab first, it contains no information at all. You must start one kind of profiling before seeing any information. We’ll start with CPU profiling. Just click on the CPU button and the instrumentation will start. During the instrumentation, the application will be blocked. After the instrumentation, you can access the application again and you will see the results of the profiling displayed in the table. Of course the profiling has an overhead on your application. Normally it’s not visible, but for certain applications, you can loose a lot of fluidity. Here are the results I have obtained with my simple application :
In my example, we can see that the waitForTimeout method takes 81.6% of the CPU time. We can also see that the notifyDecision and getSensor methods are the two next most CPU consuming methods, so perhaps it will be interesting to optimize them. You can also look at the number of invocations of each, perhaps you’ll find a method that is invoked too much of the time.
The next profiling we can do is the Memory profiling. Here again, you have to start the profiling and the instrumentation will start and during this the application will be frozen. Here are the results for my application :
Here we can see that this application stores some big double and float arrays and that EllipseIterator and BasicStroke classes take also a lot of memory space.
In both the memory and CPU profiling, you can save the results to a file to review them later. for example, you can let an application work throughout the night, save the results in the morning and examine them. Or you could create three profiling sessions and compare them.
To conclude, I have to say that this profiler is really simple but also really powerful to use. We’ve the main features we want for a profiler and the results are really good. These kind of tools can really help you to improve an application to use less CPU and memory. Of course this kind of tool doesn’t do everything, it just helps to highlight what parts of the application can be improved. The improvement part is the task of the developer and it’s not the easiest one. But having these kind of tools is a good start.