Friday, November 20, 2009

Monitoring Tomcat with Java VisualVM

One of the best kept secrets that's bundled with your Java 6 JDK is VisualVM. VisualVM is an absolutely fantastic, free monitoring tool for Java that you may not realize is right under your nose.

In a nutshell, when you fire up VisualVM it provides you with a ton of monitoring tools for everything running on the JVM. By default VisualVM will monitor the VM from which it's launched, so if for example you launch VisualVM from the bin directory of jdk1.6.0_17, then anything using that JVM will show up as a process in VisualVM that can be monitored. Note that in some cases certain processes will not appear in VisualVM, which is the real point of this post; I'll get to that in a moment.

I'm attaching a few screenshots to this post. The first shows what VisualVM looks like when I fired it up just now on my Linux laptop, the second shows a snapshot of the main monitor screen (in this case I'm monitoring Tomcat), and the third shows a snapshot of the thread monitoring screen (note the Open BlueDragon threads!). VisualVM can also take and load snapshots so you really hone in on problems at the VM level quite easily.

One major point I'd like to make is that VisualVM will monitor any Java application running on the JVM. So in the CFML world this means if you have OpenBD, Railo, or ColdFusion running, they can all be monitored quite nicely using VisualVM.

VisualVM can also monitor remote JVMs via JMX (Java Management Extensions), so if you're having trouble on a remote server and want to see what's going on, as long as the JMX ports are open and accessible you can launch VisualVM from your local machine and connect to the remote VM. Note that monitoring is a very lightweight process so VisualVM can be used to monitor production servers with virtual no impact. Visual VM also does profiling, however, which is a much more heavyweight process. It provides a huge amount of useful information, but should be used only when absolutely needed on production servers since it will have a noticeable impact on performance.

Now to the real point of this post. When I launched VisualVM on a Windows 2003 server today I was surprised that Tomcat didn't show up as a process running under the VM. Turns out that if you install Tomcat as a service, even if it's running under the same user account that you used to launch VisualVM, Tomcat won't appear by default in the VisualVM process list.

Luckily it's easy enough to resolve. Simply open the Tomcat Configuration application and add the following in the Java Options box on the Java tab:

-Dcom.sun.management.jmxremote.port=8086
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

After making this change you do have to restart Tomcat.

This will enable JMX in Tomcat and allow VisualVM to connect to it. You can choose any port you like, and note that if you want to use SSL or authentication you would set those options to true. I haven't personally messed with authentication so I'm not sure what that authenticates against, but know that if you want to have JMX available on a production system that you can secure it this way, or of course through firewall rules.

With JMX enabled in Tomcat you then go into VisualVM, add a new JMX connection, and point it to localhost:8086 (or whatever port you set JMX to run on). That's it--you're now monitoring Tomcat!

VisualVM is a great, free tool that you likely already have on your machine, so you really owe it to yourself to check it out.



14 comments:

garyrgilbert said...

I've been using jconsole to check the thread that are running on the VM, will have to check out VisualVM, thanks for posting this matt. Being able to monitor the JVM in this fashion will give us real insight on tweaking our settings.

Peter J. Farrell said...

Ok, this is too freaky Matt. I fired up VisualVM tonight because I needed to check on a possible memory leak I was seeing. The ironic part is that I didn't see that you posted this until about 30 minutes *after* I stat'ed I was using VisualVM. Ok, we think too much alike!

Matthew Woodward said...

Thanks--I've been meaning to check out jconsole more. Haven't really dug into it yet.

Matthew Woodward said...

Dang. That is a bit freaky. ;-)

Kurt Wiersma said...

I believe that VisualVM replaces jconsole as of JVM 1.6. It should have all the features of jconsole plus more is what I have heard.

Matthew Woodward said...

Thanks--good to know! I was getting everything I needed from VisualVM so I hadn't looked into other tools yet.

marcesher said...

Kurt, I'm not su sure that it's a replacement. Visually it shows most of the same stuff, albeit it not as granular. The big thing missing though are the different operations you can invoke on registered MBeans (via the 'operations' tree nodes in jconsole).I'm liking VisualVM, but I find myself in jconsole a fair amount still. However, there are plugins -- go to Tools, plugins -- and there's a jconsole plugin. But I have so far been unsuccessful at getting them to install.

yardus said...

@marcesher Once you install the VisualVM-MBeans plugin you will get all the MBeans functioanlity available in JConsole. Although the operations are not available as an MBean subtree but they are represented as the MBean subtab (an MBean has 4 subtabts - Attributes, Operations, Notifications and Metadata)

Matthew Woodward said...

Cool--thanks for the info!

marcesher said...

@yardus I did find the different plugins and am really liking jvisualvm. However, one problem I've run into -- and haven't yet spent the time to figure out the best way to solve it -- is on production servers with no external access. JVisualVM needs to go out and download those plugins, and on our production servers, I can't do that. What I don't get is why these things aren't installed by default. What's the benefit of keeping them "out there" and forcing people to fetch them?And is there a way to install them without internet access?

boron747 said...

hello, i've connected visualvm to tomcat via jmx port 8086 per above guide, however I don't see the profiler tab/option. I do see profiler tab/option for other java apps. is anyone else having this problem?

tedsteinmann said...

Great post, thank you!

techcooker said...

@marcesher never ever run profiler tool on the same machine you want to profile as profilers swallow the machine's resources, it's strongly recommended to profile servers remotely.

ARM said...

Hi,

Thanks for the post.
Exactly what I am looking for.


Thanks,
Atul