Monitor Your Applications With JConsole - Part 1

[Original pag]  

Since the release of JDK 5 the JVM uses JMX technology to provide information on performance and resource consumption of the applicationsrunning in the JVM. Together with the JConsole tool we can use this information to monitor our applications and locate problems.

To keep applications running smoothly it's important to be able to get information about the performance and resource consumption of theseapplications. This is especially the case for long running applications like websites. While we can get some information from the operatingsystem, like CPU and memory usage, we often need much more detailed information. This is why application server vendors offer specialized tooling to monitor the application server and the applications running 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 an application server or if we're using an application server that does not include such a monitoring tool? Ratherthen having to rely on the operating system and log and trace files or build our own custom monitoring code, we can use the Java ManagementExtensions (JMX) that are now part of the Java SE platform.

The Java Management Extensions (JMX) specification was created to define a standard architecture and APIs for monitoring and managing Java applications. JMX defines how an application can provide instrumentation for monitoring and managing (part of) an application by creatingso-called MBeans and registering them in a MBean server. This MBean server is part of to a JMX agent togetherwith a minimum number of agent services and at least one protocol adapter or connector.  




  The protocol adapters and connectors enable remote management applications to connect to the JMX agent using differentcommunication 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 is not as sophisticated as some of the tooling offered by application server vendors, it does give some valuable informationabout resource consumption and access to the MBeans running in the JVM. By adding MBeans to our 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 see later on.  


  Since we'll be using the version of JConsole that 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 will not be available to you. Note that it's possible to use JConsole from JDK 6 to connect to a JDK 5JVM, in which case you'll only be missing the features added to the JVM in release 6.

  • The Summary tab has been replaced with the Overview tab shown above. This tab shows the graphs from the Memory,Threads and Classes tabs and a graph of the CPU usage of the JVM which is not available in JDK 5. The informationon the Summary tab has been merged with the VM tab, now called VM Summary
  • JConsole now has a Plugin API 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 be started with -Dcom.sun.management.jmxremote.

If you're connecting to a Java 6 JVM, you'll also find a new HotSpot Diagnostics MBean on the MBeans tab. This new MBeanprovides an API to request heap dump at runtime and change the setting of certain VM options.

As an example we'll be using JConsole to monitor the memory consumption of a web application running in 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 that it uses to register its MBeans with, we still need the com.sun.management.jmxremote system setting even if we're using JDK 6. If we don't do this JBoss will generate a ClassNotFoundException if you try to connect with JConsole. Another result of the fact that JBoss uses it's own MBean server is that the JBoss MBeans will not beavailable on the MBeans tab. If you want these MBeans available in JConsole and you're using JBoss 4.0.3 or later, you 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 errors whenconnecting to JBoss. Simply reconnecting usually solves the problem, but I suggest you only use 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 configuredall other JConsole related settings. This 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 will open with the New Connection dialogthat lists a Java processes running on the local machine. Java processes that do not support remote JMX access will be grayed out. Selectthe JBoss process and click Connect.  



JConsole will connect to the JVM running JBoss and we can select the Memory tab to monitor the memory consumption of our applicationrunning 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 between a certain upper and lowerlimit.  



On the other hand if there is a memory leak in our application, we'll slowly see the memory usage growing over time. Depending on how muchmemory the application is leaking per request and the load on the application, this may take quite some time. It may even take hours beforeyou see a change in the memory usage. For example the screenshot below shows an increase in the heap memory usage after about 20 minutes even though I've created a fairly large memory leak for this test.




Because objects that haven't been garbage collected for a longer period of time eventually end up in the tenured generation of the heap, the graph for this part of the heap shows our problem even more clearly. See Resources for more informationon garbage collection.  



If you think you found a memory leak, make sure that you've tested long enough. Not only may it take a while before a memory leak will showitself in the graph, but short term changes in the size of the heap may also look like a memory leak when over a longer period of time theheap never grows beyond a certain limit. As an example of this look at the screenshot below. This looks like an obvious memory leak, right?



  As it turns out this is not a memory leak, but caused by the fact that in this test we create a session for every user. Since the sessionswill not time out until after 30 minutes of inactivity, we see a large growth of the heap in the first 30 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 an increase in the number of threads.

If you do think you've found a memory leak try to test a smaller part of the application to zero in on 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 objects are responsiblefor allocating so much memory.

Monitoring memory consumptions is just one of the useful things you can do with JConsole. You 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'reusing JDK 6 you can also write your own plugins for JConsole to add the functionality you need.

JDK 6 comes with a demo plugin called JTop that adds an extra tab to JConsole that shows the threads that have been 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 see the Resources section.

As we've seen JConsole can be a useful tool to monitor the health of our applications and diagnose problems on our local machine. Whilethis is no problem during development, using JConsole locally in a production environment is not recommended because of the significant resourceusage of JConsole itself. So to monitor applications in a production environment we should use the remote monitoring functionality of JConsole, which we'll be looking at in Part 2.


Resources



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值