Monitoring and diagnosing performance issues in Java SE 6

Source: Internet
Author: User
Tags stack trace high cpu usage jconsole java se

Java™platform, Standard Edition 6 (Java SE) focuses on improving performance, providing enhanced tools to manage and monitor applications and diagnose common problems. This article introduces the basics of monitoring and management in the Java SE platform and provides detailed information about the enhancements in Java SE 6.

Java SE 6 provides an in-depth look at performance, using enhanced tools to manage and monitor applications and diagnose common problems. These improvements include:

    • Monitoring and managing API enhancements
    • Formal support for enhanced graphical monitoring tools JConsole
    • Provides enhanced Java virtual machine (JVM) testing tools

This article describes the basics of monitoring and management for the Java SE platform, and details performance monitoring and management enhancements in the latest release. The diagnostic and troubleshooting tools provided by the Java SE 6 platform are also described.

To benefit from this article, you should be very familiar with the monitoring and management features introduced in earlier versions of Java SE. For more background information, see Resources.

Monitoring and Managing APIs

The package introduced in Java SE 5 java.lang.management defines 9 Mbean, called a platform Mbean or MXBean(see Resources). Each MXBean encapsulates a separate functional area of the JVM. Beginning with Java SE 5, the JVM contains a built-in Mbean server called the platform Mbean server . MBeans is located and managed by it. Table 1 lists the 9 Mxbeans in the Java platform:

Table 1. Platform MBean
Management Interface Managed Resources
ClassLoadingMXBean Class Loader
CompilationMXBean Compiler
MemoryMXBean Memory
ThreadMXBean Thread
RuntimeMXBean Run-time
OperatingSystemMXBean Operating system
GarbageCollectorMXBean Garbage collector
MemoryManagerMXBean Memory Manager
MemoryPoolMXBean Memory pool

Any application can obtain and use the platform MBean provided by the JVM by acquiring an instance of the target bean and invoking the appropriate method. MXBean can be used to monitor the behavior of local and remote JVMS and obtain relevant information.

Platform MBean provides access to information, such as the number of classes loaded, JVM uptime, memory consumption, number of running threads, and thread contention statistics.

You can use one of the following two ways to monitor and manage JVM resources:

    • Direct Access MXBean Interface
    • Using MBeanServer interfaces for indirect access
Direct access using the MXBean interface

You can obtain an instance from a static factory method that MXBean gives you direct access to the interface of the JVM running locally MXBean . ManagementFactoryclass provides static factory methods to obtain MXBean. Listing 1 shows how to use the factory to obtain RuntimeMXBean and obtain VmVendor the values of one of the standard properties:

Listing 1. Direct access to MXBean
Runtimemxbean Mxbean = Managementfactory.getruntimemxbean ();//Get The standard attribute "Vmvendor" String vendor = Mxbea N.getvmvendor ();
Indirect access using the Mbeanserver interface

The Platform MBeanServer interface MXBeanServerConnection allows you to connect to a remote JVM and access the MXBean running on those platforms. You can ManagementFactory getPlatformMBeanServer access the platform MBean server by using the methods of the class. Listing 2 shows how to get the values that run on the remote JVM RuntimeMXBean and get VmVendor the properties:

Listing 2. Indirect access to MXBean
Mbeanserverconnection serverconn;try {   //connect to a remote VM using JMX RMI   jmxserviceurl url = new Jmxserviceur L ("service:jmx:rmi:///jndi/rmi://<addr>");   Jmxconnector jmxconnector = jmxconnectorfactory.connect (URL);   Serverconn = Jmxconnector.getmbeanserverconnection ();   ObjectName objname = new    ObjectName (managementfactory.runtime_mxbean_name);   Get Standard attribute "Vmvendor"   String vendor =    (String) Serverconn.getattribute (objname, "Vmvendor");} catch (...) { }

For java.lang.management more detailed information about Mxbeans and APIs, see Resources.

API Enhancements in Java SE 6

Java SE 5 introduces a java.util.concurrent.locks package that provides a framework for locking and waiting conditions. This framework differs from the built-in synchronization support of Java and allows for greater flexibility in the use of locks.

Java SE 6 java.lang.management adds support for the package java.util.concurrent.locks . This includes new classes that provide lock information, and ThreadInfo enhancements to, ThreadMXBean and OperatingSystemMXBean interfaces.

Java SE 6 introduces two new classes:

    • LockInfoContains information about the lock.
    • MonitorInfoExtended LockInfo and contains information about object monitoring locks.

ThreadInfoClass takes advantage of these new objects and introduces three new methods:

    • getLockInfo()Returns the LockInfo object, the given thread is blocked to wait for the object.
    • getLockedMonitors()Returns the object currently locked by a given thread MonitorInfo .
    • getLockedSynchronizers()Returns an LockInfo object that provides an available synchronization program that is currently locked by a given thread.

In Java SE 5, the ThreadMXBean.getThreadInfo method reports only the object monitor that the thread is waiting to acquire or the monitor that is blocked. These methods have been enhanced by Java SE 6 to report that threads are waiting to be acquired AbstractOwnableSynchronizer .

4 new methods are added to the ThreadMXBean interface:

    • isObjectMonitorUsageSupported()Tests whether the virtual machine supports monitoring the use of Object monitor.
    • isSynchronizerUsageSupported()Test the virtual machine for monitoring the available Synchronizer usage.
    • findDeadlockedThreads()Returns an array of IDs for threads that are in a deadlock state. The deadlock thread is blocked from entering the object monitor or synchronization program.
    • dumpAllThreads()Returns stack traces and synchronization information for all active threads.

Finally, the OperatingSystemMXBean interface is updated to include the getSystemLoadAverage() method, which can return the system load average for the previous minute.

In addition to providing programmatic support, Java SE 6 includes a number of diagnostic and troubleshooting tools that can be used to detect problems and monitor JVM resource usage. The next two sections describe and demonstrate several of the diagnostic tools available.

Java Monitoring and Management Console (JConsole)

Java SE 6 provides formal support for JConsole, which is the monitoring and management console introduced in Java 5 SE. JConsole enables you to monitor various JVM resource statistics at run time. This feature is especially useful for detecting deadlocks, lock contention, memory leaks, and looping threads. It can be connected to a local or remote JVM and is available for monitoring:

    • Thread state (including associated locks)
    • Memory usage
    • Garbage collection
    • Run-time information
    • JVM Information

The following subsections describe the enhancements that Java SE 6 makes to JConsole. For more information about how to start and use JConsole, see Resources.

Attach API Support

Starting with Java SE 6, JConsole implements the new Attach API. The API consists of two packages- com.sun.tools.attach and com.sun.tools.attach.spi -you can dynamically connect the application implementation process to the target virtual machine and run the agent within the JVM.

In the past, applications that wanted to use JConsole for monitoring were required to start with an -Dcom.sun.management.jmxremote option, and now the application no longer needed to start with this option. Support for dynamic connections enables JConsole to monitor any application that supports the Attach API. JConsole will automatically detect compatible applications at startup.

Enhanced UI and MBean representations

Java SE 6 has been updated with JConsole, which makes it feel like a Windows® operating system or GNOME desktop (depending on the platform you are running). The screen shown later in this article uses Windows XP and shows different UI features than previous versions.

Once started and associated with the application, a JConsole view consisting of 6 tabs is displayed. Each tab represents a different JVM resource or a set of resources:

    • Overview
    • Memory
    • Threads
    • Classes
    • VM Summary
    • MBeans

The Overview tab displays information about memory usage, threads, classes, and CPU usage in a graphical format. The Overview tab displays a set of related information in a page that previously needed to be toggled between tabs to display. Figure 1 shows the Overview tab for a sample application:

Figure 1. JConsole Overview tab

Click to view larger image

The Overview tab shows 4 graphs that reflect VM resource usage and a pick list to modify the time range in which the results are displayed. The first image, heap memory Usage, shows the amount of memory in the heap, increasing in gigabytes per unit over time. This diagram helps detect memory leaks. If there is a memory leak in your application, heap memory usage will grow steadily over time.

Threads graphics grow over time to depict the number of active threads, while the Classes graph depicts the number of classes loaded. The CPU usage graph describes the percentage of CPU usage that the application has at different points in its life cycle.

The VM Summary tab shown in Figure 2 is another new enhancement to the Java SE 6 release. It provides detailed information about the JVM, including the total elapsed time, thread information, loaded classes, memory statistics, garbage collection, and operating system information.

Figure 2. JConsole VM Summary Tab

Click to view larger image

The MBeans tab is enhanced to make it easier to access the operations and properties of an MBean. It shows all the information that is registered to the MBean in the platform, which provides access to all platform MBeans. The tree structure on the left shows all the MBean currently running. When an MBean is selected, its mbeaninfo and descriptors are displayed in the table on the right, as shown in 3:

Figure 3. JConsole MBean Tab

Selecting the Attributes node will display all the properties of the MBean, with the threading Mbean shown in 4:

Figure 4. MBean Properties

Note that the properties and their values on the right side of the box correspond to the property values obtained by the API of the package described earlier, and java.lang.management ThreadMXBean additional information about the listed properties can be obtained by double-clicking the property value, and only the value of the property displayed in bold can be expanded. For example, double-clicking the AllThreadIds value will show the thread ID of 22 threads, as shown in 5:

Figure 5. Expanded property values

Writable properties are displayed in blue and can be edited by clicking them and entering new values. For example, the properties shown in Figure 5 ThreadContentionMonitoringAvailable can be edited in this way in the JConsole view.

Selecting the Operations node in the tree structure on the left displays the actions associated with the MBean. The MBean action is displayed as a button in the area on the right, and the specified method is invoked when clicked. Figure 6 shows what you can ThreadMXBean do with it:

Figure 6. Mbean Operation hotspot Diagnostic MBean

In Java SE 6, JConsole provides support for the HotSpot Diagnostic MBean. This introduced MBean allows you to perform on-the-spot diagnostic operations. Its API allows users to perform heap dumps at run time and set additional VM options. You can access the HotSpot Diagnostic Mbean from the MBean tab by expanding the com.sun.management node and selecting HotSpotDiagnostic . Figure 7 shows the methods available for the HotSpot Diagnostic MBean:

Figure 7. HotSpot Diagnostic Mbeanjconsole Plugin support

Starting with Java SE 6, JConsole provides plug-in support that allows you to build your own plug-in to run with JConsole. For example, you can add a custom tab to the JConsole view to access application-specific MBeans and perform your own monitoring activities.

You must extend the abstract com.sun.tools.jconsole.JConsolePlugin class to create a custom JConsole plug-in. You will implement 2 methods for the plug-in to correctly display it in the JConsole view:

    • newSwingWorker()Returns an SwingWorker object that performs a GUI update on the plug-in.
    • getTabs()Returns a tab graph that is added to the JConsole window.

JConsole uses its service provider mechanism to detect and load all plug-in classes. Therefore, you must provide a plug-in class for the JAR file that contains the Meta-inf/services/com.sun.tools.jconsole.jconsoleplugin file. The file should contain a set of fully compliant plug-in class names, with each line displaying a class name. To mount the new plug-in to the JConsole view, run JConsole on the command line using the following command:

Plugin_path

The plugin_path in the command refers to the directory path or the archive of the JConsole plugin. You can specify more than one path.

Java SE 6 comes with an example JConsole plug-in called Jtop. Jtop Displays the CPU usage of the running thread within the current application. To run JConsole and Jtop, execute the following command:

Jconsole-Pluginpath Java_home/demo/management/jtop/jtop.jar

Figure 8 shows an example of JConsole, which selects the Jtop tab. The left column shows the names of all running threads. For each thread, its CPU usage and thread state are displayed. When the statistics change, the view is automatically refreshed. The Jtop plugin can be used to identify threads with high CPU usage.

Figure 8. JConsole jtop plugin monitoring and troubleshooting tools

In addition to JConsole, Java SE 6 also provides support for other command-line tools. These diagnostic tools can connect to any application without requiring the application to start in a special mode. They enable you to get more application information to determine if it works as expected. Note that the listed tools are still experimental, and future versions of Java SE may not be able to provide full support.

Monitoring Tools

Java SE 6 includes three command-line tools, as shown in table 2, which can be used to monitor JVM performance statistics:

Table 2. Monitoring Tools
Tools Description
jps JVM Process Status Tool
jstat JVM Statistics Monitoring Tool
jstatd JVM Jstat Daemon

jpsTool lists the virtual machines for the current user of the target system. This is particularly useful for environments that use the JNI invocation API instead of the standard Java launcher to launch VMS. In these environments, it is often difficult to identify Java processes from the list of processes. jpstool solves this problem.

The following example demonstrates the jps use of a tool. At the command line jps , the tool lists the virtual machine and process ID for users who have access, as shown in the example in Listing 3:

Listing 3. Using the JPS tool
$ jps16217 MyApplication16342 JPS

jstatThe tool uses the JVM's built-in testing tools to provide information about the performance and resource consumption of the applications that are running. This tool helps diagnose performance issues, specific problems related to heap size and garbage collection.

jstatdThe daemon is a remote Method invocation (RMI) server application that monitors the creation and termination of the JVM and provides an interface to allow remote monitoring tools to connect to the JVM running on the local host. For example, this daemon allows jps a tool to list processes in a remote system.

For more documentation and examples of using these tools, see resources.

Troubleshooting tools

Java SE 6 also provides troubleshooting tools, as shown in table 3, which help you identify the parts of an application that are running exceptions:

Table 3. Troubleshooting tools
Tools Description
jinfo Configuration information
jhat Heap Dump Browser
jmap Memory mapping
jsadebugd Service Capability Agent Debug Program
jstack Stack trace

jinfoThe command-line tool obtains configuration information from a running Java process or crash dump (crash dump) and prints the system properties and the tags that are used to start the virtual machine.

jhatTool provides a convenient way to browse the object structure in a heap snapshot. This tool, introduced in the Java SE 6 release, replaces the HEAP analysis tool (HAT) to help detect memory leaks.

jmapThe command-line tool prints memory-related statistics for a running VM or core file. The tool can also use the jsadebugd daemon to query a process or core file in a remote machine. The jmap tool detects if the completion device is overused, which can cause OutOfMemoryError errors.

serviceability Agent Debug Daemon ( jsadebugd ) connects to a Java process or a core file and acts as a debug server. The tool is currently available only for Solaris OS and Linux®. jstackremote clients, such as, jmap and jinfo such, can connect to such servers through Java RMI.

jstackThe command-line tool connects to the specified process or core file and prints stack trace information for all threads connected to the virtual machine, including Java threads and internal VM threads, and sometimes local stack frames. The tool also performs deadlock detection. It uses jsadebugd daemons to query processes or core files on a remote machine. jstacktool can be used to detect deadlock problems.

For more documentation and examples of use of these tools, refer to resources.

Conclusion

The Java 6 platform provides some enhancements to VM testing, management APIs, and JDK tools to help effectively identify and diagnose performance and memory problems in Java applications. This article describes the enhancements added to the Java SE monitoring and management framework and the diagnostic command-line tools available to developers.

The average performance of Java applications has improved steadily over time. Java performance can now be comparable to C or C + + with Java SE 6 versions. In many cases, Java code is running significantly faster. You can also use the tools described in this article for better performance tuning. Please try it yourself. We can ensure that you find application optimizations that have never been done before.

Monitoring and diagnosing performance issues in Java SE 6

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.