DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Profile Your Applications with Java VisualVM

Profile Your Applications with Java VisualVM

Baptiste Wicht user avatar by
Baptiste Wicht
·
Jul. 13, 10 · Interview
Like (0)
Save
Tweet
Share
54.44K Views

Join the DZone community and get the full member experience.

Join For Free

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

application Java (programming language) Profile (engineering) Memory (storage engine)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • 11 Observability Tools You Should Know
  • Spring Boot vs Eclipse Micro Profile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Multi-Cloud Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: