5. instruct you to grasp Java performance monitoring from 51 testing)

Source: Internet
Author: User
Tags visualvm jconsole netbeans

Many developers think they understandJavaProgramming, the fact is that most developers only understand the Java platform, and what they learn is enough to copeWork. The author will explore the core functions of the Java platform in depth, reveal some little-known facts, and help you solve the most difficult programming difficulties.

When the application performance is compromised, most developers are panic, which is reasonable. Tracing Java application bottlenecks has always been troublesome because Java virtual machines have a black box effect and Java platform analysis tools have always had defects.

However, with the introduction of jconsole in Java 5, everything has changed. Jconsole is a built-in JAVA Performance Analyzer that can be run from the command line or in guishell. It is not perfect, but when the top boss asks you about performance, it is more than enough to deal with it-it is much better than querying papagoogle.

We will show you five methods so that you can easily monitor Java application performance and track code in Java using the jconsole (or its higher-end "close friend" visualvm.

1. JDK with Analyzer

Many developers do not realize that JDK contains a analyzer from Java 5. Jconsole (or the latest version of the Java platform, visualvm) is a built-in analyzer, which is as easy to start as the Java compiler. If it is started from the command line, make JDK on the path and run jconsole. If you start from guishell, find the JDK installation path, open the bin folder, and double-click jconsole.

When the analysis tool pops up (depending on the running Java version and the number of running Java programs), a dialog box may appear asking you to enter a process URL to connect, it is also possible to list many different local Java processes (sometimes including the jconsole process itself) for connection.

Use jconsole for work

In Java 5, the Java Process is not set as the default analysis, but through a command line parameter -- DCOM. sun. management. jmxremote -- tell the Java 5 VM to open the connection at startup so that the analyzer can find them. When a process is picked up by jconsole, you can only double-click it to start analysis.

Analyzer has its own overhead, so the best way is to spend some time figuring out what overhead is. The simplest way to discover jconsole overhead is to first run an application on its own, then run it in the analyzer, and measure the difference. (The application cannot be too large or too small. I like to use the swingset2 sample that comes with JDK .) Therefore, I use-verbose: GC to try to run swingset2 to view garbage collection cleanup, then run the same application and connect the jconsole analyzer to it. After the jconsole connection is complete, a stable GC cleaning flow appears, otherwise it will not appear. This is the performance overhead of the analyzer.

Jconsole or visualvm?

Jconsole has been released along with the Java platform version since Java 5, and visualvm is a analyzer upgraded Based on netbeans. It was first released in version 12 of Java 6. Most of them have not been updated to Java 6, so this articleArticleThis section describes jconsole. However, most techniques are related to both analyzers.

2. remote connection process

BecauseWebThe application analysis tool uses a socket for connectivity analysis. You only need to configure jconsole (or jvmti-based analyzer) with a few configurations ), monitor/analyze applications that run remotely.

If Tomcat runs on a machine named "webserve" and the JVM has started JMX and listens to port 9004 from jconsole (or any JMX client) to connect to it, you need a jmx url "service: JMX: RMI: // JNDI/RMI: // webserver: 9004/jmxrmi ".

Basically, to analyze an application server running in a remote data center, you only need a jmx url.

3. Tracking statistics

Jconsole has many tabs that are useful for collecting statistics, including:

◆ Memory: tracks the activity of each heap in the JVM garbage collector.

◆ Threads: Check the current thread activity in the target JVM.

◆ Classes: Observe the total number of classes loaded by the VM.

These tabs (and related charts) are provided by JMX objects registered on the JMX server by each Java 5 or higher Vm and are built into the JVM. A complete list of available beans in a given JVM is listed on the mbeans tab, which includes metadata and a limited user interface for viewing data or performing operations. (However, registration notifications are outside the jconsole user interface .)

Usage Statistics

Assume that a tomcat process is killed by an outofmemoryerror. To find out what happened, open the jconsole and click the classes tab to view the class count once in a while. If the number of instances increases steadily, you can assume that the application server or your code has a classloader vulnerability, which will soon exhaust the permgen space. To further confirm the problem, see the memory tab.

Do not be typical

Common Responses to performance problems in application code are diverse but predictable. Early Java programmers may be very angry with the old IDE and begin to review the code in the main part of the code base to find the familiar "red mark" in the source code ", such as Asynchronous blocks and object quotas. With the increase in programming experience, developers may carefully study the-x flag supported by JVM to find a way to optimize the garbage collector. Of course, for beginners, go directly to Google for query, hoping that others will find the JVM's magic "make it go fast" conversion to avoid code rewriting.

In essence, these methods are all at risk. The most effective response to a performance problem is to use a analyzer-now they are built into the Java platform, and we have no reason not to do so!

4. Create a heap dump for offline analysis

In the production environment, everything is fast, and you may not have time to spend on your application analyzer. On the contrary, you can save a snapshot for each event in the Java environment and then view it again. You can also do this in jconsole, and even do better in visualvm.

Find the mbeans tab, open the com. Sun. Management node, and then the hotspotdiagnostic node. Now select operations. Pay attention to the "dumpheap" button in the right panel. If you pass a file name to dumpheap in the first ("string") input box, it will take a snapshot of the entire JVM heap and dump it to that file.

Later, you can use a variety of commercial analyzers to analyze files, or use visualvm to analyze snapshots. (Remember, visualvm is available in Java 6 and downloaded separately .)

5. jconsole is not uncertain

As a analyzer utility, jconsole is excellent, but there are still better tools. Some analysis plug-ins come with analyzer or a clever user interface. By default, more data is tracked than the jconsole.

What really appeals to the jconsole is that the entire program is written in "normal old-style Java", which means that any Java developer can write such a utility. In fact, JDK even includes how to create a plug-in to customize the jconsole example. The visualvm built on top of netbeans further extends the plug-in concept.

If the jconsole (or visualvm, or any other tool) does not meet your needs, or you cannot track what you want, or follow your own method, you can write your own tools. If you think Java code is very troublesome, groovy, jruby, or many other JVM languages can help you do it faster.

What you really need is a fast and rough (quick-and-dirty) command line tool connected by JVM, which can precisely track the data you are interested in the way you want.

 

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.