I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

Profile Your Applications with Java VisualVM

07.13.2010
| 28338 views |
  • submit to reddit

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.

VisualVM start logoVisualVM start logo

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 :

VisualVM Main FrameVisualVM Main Frame

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 :

VisualVm The applicationVisualVm The application

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 :

VisualVM Overview tabVisualVM Overview tab

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 :

VisualVM Monitor tabVisualVM 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 :

VisualVM Threads tabVisualVM Threads tab

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 :

VisualVM Profiler tab VisualVM Profiler tab

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 :

VisualVM CPU profilingVisualVM CPU profiling

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 :

VisualVM Memory profilingVisualVM Memory profiling

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.

From http://www.baptiste-wicht.com/2010/07/profile-applications-java-visualvm

Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Thomas Eichberger replied on Wed, 2010/07/14 - 12:35am

Nothing new here.

Siva Gurusamy replied on Wed, 2010/07/14 - 10:29pm

I coudnt find profiler tab in visualvm in windows OS. Should I install any plugin for visualvm? Thanks siva

Baptiste Wicht replied on Thu, 2010/07/15 - 3:16pm in response to: Siva Gurusamy

No, normally there is nothing to install, but sometimes you have to install some library. What kind of applications do you try to profile ? Have you some errors or warning at the start of the application or during calibration ? If yes, post the error or warning, that could help.

Ahmed El Amine replied on Wed, 2011/03/09 - 5:37am

can't find the jvsualVM exe !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.