[Original Pag]
Since the release of JDK 5 The JVM uses JMX technology to provide information on performance and resource consumption of T He applicationsrunning in the JVM. Together with the Jconsole tool we can-information to monitor our applications and locate problems.
To keep applications running smoothly it's important to is able to get information about the performance and resource C Onsumption of Theseapplications. This is especially the case to long running applications like websites. While we can get some information to the operatingsystem, like CPUs and memory usage, we often need much more detailed in Formation. This is why application server vendors an offer specialized tooling to monitor the application server and the applications Ru Nning inside the application server. A good example of such a tool is IBM's Tivoli performance Viewer, which enables us to monitor WebSphere application Server And the applications running inside of it.
But What if we ' re not using a application server or if we ' re using a application server that does not include such a mon Itoring tool? Ratherthen has to rely on the operating system and log and trace files or builds our own custom monitoring code, we can Use the Java managementextensions (JMX), which is are now part of the Java SE platform.
The Java Management Extensions (JMX) specification is created to define a standard architecture and APIs for monitoring a ND managing Java applications. JMX defines how a application can provide instrumentation for monitoring and managing (part of) a application by Creatin Gso-called Mbeans and registering them in a MBean server. This MBean server are part of to a JMX agent Togetherwith a minimum number of agent services and at least one protocol ADAP ter or connector.
The Protocol adapters and connectors enable remote management applications to connect to the JMX agent using Differentcomm Unication protocols like RMI, HTTP or SNMP. For example the Jconsole application that's packaged with the Sun JDK since release 5 uses RMI to connect to the JMX agent .
While Jconsole isn't as sophisticated as some of the tooling offered by Application server vendors, it does give some Val Uable informationabout resource consumption and access to the Mbeans the JVM. By adding Mbeans to my applications or using Anapplication server like JBoss that supports JMX, we can easily extend the Out-of-the-box functionality. JDK 6 Even enables us to add functionality to the Jconsole application itself via plugins, as we ll-later on.
since We'll be using the version of Jconsole to comes with JDK 6, let's take a quick look at the changes From JDK 5. If you ' re still using JDK 5, the features listed below won't be available to. Note that it's possible to use Jconsole from JDK 6 to connect to a JDK 5JVM, in which case you'll be missing the feat Ures added to the JVM in release 6. The Summary tab has been replaced with the Overview tab shown. This tab shows the graphs from the Memory,threads and Classes tabs and a graph of the CPU usage of the JVM which are not AV Ailable in JDK 5. The Informationon the Summary tab has been merged with the VM tab and now called VM Summary Jconsole now has a Plugin APIs for adding custom functionality. Jconsole can now use the attach API to dynamically attach to a JVM. This requires a Java 6 JVM. To connect to a Java 5 JVM, THEJVM must is started with-dcom.sun.management.jmxremote.
If you are connecting to a Java 6 JVM, "ll also find a new HotSpot diagnostics MBean on the Mbeans tab." This new mbeanprovides a API to request heap dump at runtime and the setting of the certain VM options.
As an example we are using Jconsole to monitor the memory consumption of a Web application running into JBoss 4.0.5 on our Local machine. We ' llbe looking at monitoring applications on a remote machine in part 2.
Because JBoss has its own MBean server so it uses to register its Mbeans with, we still need the COM.SUN.MANAGEMENT.JMXR emote system setting even if we ' re using JDK 6. If we don ' t do this JBoss would generate a classnotfoundexception if you try to connect with Jconsole. Another result of the fact that JBoss uses it's own MBean server is and that the JBoss Mbeans would not be beavailable on the Mbea NS tab. If you want this Mbeans available in Jconsole and your re using JBoss 4.0.3 or later, can use thefollowing JVM options In the JBoss startup script:
-djavax.management.builder.initial=org.jboss.system.server.jmx.mbeanserverbuilderimpl
- Djboss.platform.mbeanserver
-dcom.sun.management.jmxremote
Although this solution enables us to use both the JBoss and Java platform MBean Server This also frequently generates Erro RS whenconnecting to JBoss. Simply reconnecting usually solves the problem, but I suggest you with this solution if you really need it. I alsorecommend that you start with the platform Mbean server and add, the JBoss Mbean server when you have successfully co Nfiguredall other Jconsole related settings. This is way you'll avoid debugging connection problems caused by the JBoss MBean server, Especiallywhen connecting to remote Applications.
With the startup script modified, we can now start JBoss and startup Jconsole. Jconsole'll open with the New Connection dialogthat lists a Java processes running on the local machine. Java processes that does not support remote JMX access is grayed out. Selectthe JBoss process and click Connect.
Jconsole'll connect to the JVM running JBoss and we can select the Memory tab to monitor the Memory consumption of We A Pplicationrunning in JBoss. To check for memory leaks in our application we can start a load test to create a constant load on the application. If thefunctionality we ' re testing during the load test does not contain a memory leak the memory usage should stay A certain upper and lowerlimit.
On the other hand if there are a memory leak in our application, we'll slowly you to the memory usage to growing. Depending on how muchmemory the application are leaking per request and the "load on" the application, this could take quite so Me time. It may even take hours beforeyou to the memory usage. For example the screenshot below shows a increase in the heap memory-after about usage minutes I ' ve create D A fairly large memory leak for this test.
Because objects that haven ' t been garbage collected for a longer period of time eventually to the tenured N of the heap, the graph for this part of the heap shows we problem even more clearly. Informationon garbage collection.
If you are found a memory leak, make sure ' ve tested long enough. Not only could it take a while before a memory leak would showitself in the graph, but short term changes in the size of the Heap may also look like a memory leak while over a longer period of the time Theheap never grows the beyond a certain limit. As a example of this look at the screenshot below. This is looks like a obvious memory leak, right?
As it turns out of the memory leak, but caused by the fact so in this test we create session for every user. Since the Sessionswill not-until after minutes of inactivity, we have a large growth of the heap in the 3 0 minutes of our test. So make sureyou ' ve tested long enough and try to eliminate other causes for memory usages as much as possible. This includes sessions, but also try tokeep the number of threads constant by using a constant load. You can use the Threads tab to check if sudden jumps in memory usagecoincide with a increase in the number of Threads.
If you don't have the ' ve found a memory leak try to test a smaller part of the application to zero in the location of the Problem. You canalso make a heapdump using the HotSpot diagnostics MBean and analyze it with the Jhat tool to find out which object s are responsiblefor allocating so much memory.
Monitoring memory consumptions is just one of the useful things your can do with jconsole. Can monitor CPU usage, detect deadlocks on thethreads tab or use the Mbeans tab to monitor or manage specific parts of The JVM or the application running in it. If you are reusing JDK 6 You can also write your own plugins for Jconsole to add the functionality.
JDK 6 comes with a demo plugin called Jtop this adds a extra tab to jconsole this shows the threads that have using The most CPU timesimilar to the top command in Linux. Use the following command to start jconsole with this command:
<jdk6>/bin/jconsole-pluginpath <jdk6>/demo/management/jtop/jtop.jar
For more examples of jconsole plugins to the Resources section.
As we ' ve seen jconsole can be a useful tool to monitor the health of my we applications and diagnose problems on our local m Achine. Whilethis is no problem during development, using jconsole locally in a production environment are not recommended Of the significant resourceusage of Jconsole itself. So to monitor applications in a production environment we should use the remote monitoring functionality of Jconsole, whic H we ' ll be looking at the Part 2.
Resources Download the latest Sun JDK including Jconsole from:java.sun.com. For more about JMX visit the JMX documentation page. For more examples the how to use Jconsole to monitor and manage applications read monitoring and Management Using JMX. For more information about configuring JBoss for Jconsole visit thejbossmbeansinjconsolepage of the JBoss Wiki. To learn more about the "tools" to "combination with jconsole to diagnose common problems in Java applications Readmon Itoring and managing Java SE 6 Platform applications. For more information about garbage collection in the HotSpot JVM read Java SE 6 HotSpot Virtual Machine Garbage Tuning. For more examples of jconsole plugins check out thescript shell pluginand theper-thread CPU usage.