Java performance monitoring

Source: Internet
Author: User

Full-featured built-in analyzer such as jconsole and visualvm sometimes cost more than they cost performance-especially in systems running on production software. Therefore, in the 2nd articles focusing on Java performance monitoring, I will introduce five command line analysis tools, allowing developers to focus only on one aspect of the running Java Process.

JDK includes many command line utilities that can be used to monitor and manage Java application performance. Although most of these applications are labeled as "experimental" and technically not supported, they are useful. You can use jvmti or jdi as the seed material of some or even specific-purpose tools (see
References.

1. JPs (sun. Tools. JPS)

Many command line tools require you to identify the Java Process you want to monitor. This is no big difference from similar tools that monitor local operating system processes and require a program identification tool.

The "vmid" identifier is not always the same as the local operating system process identifier ("PID ").jpsThe reason for the utility.

Use JPs in Java Process

Similar to configuring most JDK tools and all the tools mentioned in this articlejpsIt is usually a thin package around the Java class or the set of classes that execute most of the work. In Windows, these tools are. EXE file, using the JNI invocation API to directly call the class mentioned above; in UNIX environments, most tools are a shell script symbolic link, this script uses the specified correct class name to start a Common Startup Program.

If you want to usejps(Or any other tool) function-ant script-called only on the "Main" Class of each tool
main()
Relatively easy. To simplify the reference, the class name appears in parentheses after each tool name.

jps-The name indicates what is found on most Unix systemspsUtility-tells us the jvmid for running the Java application. As the name suggests,jpsReturns the vmid of all detected Java processes running on the specified machine. If
jpsThe absence of a process does not mean that the JAVA process cannot be attached or researched, but does not publicize its availability.

If a Java Process is found,jpsThe command lines that enable it are listed. This method of distinguishing Java processes is very important, because as long as the operating system is involved, all Java processes are collectively referred to asjava". In most cases, vmid is an important number worth noting.

Start with Analyzer

The simplest way to start with an analysis utility is to usedemo/jfc/SwingSet2Swingset2 found in demonstrates the same demo program. In this way, the program may be suspended as the background/monitoring program. After you understand the tool and its fees, you can try it out in the actual program.

Load the demo application and runjpsAnd pay attention to the returnedvmid. To achieve better results, use-Dcom.sun.management.jmxremoteThe property set starts the Java Process. If this setting is not used, some of the data collected by some of the following tools may be unavailable.

Back to Top

2. jstat (sun. Tools. jstat)

jstatUtilities can be used to collect a variety of different statistical data.jstatStatistical data is classified into "options". These options are specified as the first parameter in the command line. For JDK 1.6, you can use the command
-optionsRunjstatView the list of available options. Some options are shown in Listing 1:

Listing 1. jstat options

-class-compiler-gc-gccapacity-gccause-gcnew-gcnewcapacity-gcold-gcoldcapacity-gcpermcapacity-gcutil-printcompilation 

JDK record of the utility (see
References) will tell you the content returned by each option in Listing 1, but most of them are the performance information of collectors or their components used to collect garbage.-classOption displays the loaded and unloaded classes (make it a test application server or code)
ClassLoaderImportant utility leaked, and-compilerAnd-printcompilationInformation about the hotspot JIT compiler is displayed.

By default,jstatThe information is displayed when you check the information. If you want to take a snapshot at intervals-optionsSpecify the interval in milliseconds after the command.jstatSnapshots of monitoring process information are continuously displayed. If you want
jstatSpecify a specific number of snapshots before termination, after the interval/time value.

If 5756 is the vmid of the swingset2 program that started several minutes ago, the following command will telljstatExecute a GC snapshot dump for 10 generations every 250 milliseconds, and then stop:

jstat -gc 5756 250 10

Note that Sun (now Oracle) reserves the right to change the output of various options or even the options themselves without any prior notice. This is a disadvantage of using unsupported utilities. See javadocs for more information.
jstatOutput all details of each column.

Back to Top

3. jstack (sun. Tools. jstack)

Understanding the internal situation of Java processes and their corresponding execution threads is a common diagnostic challenge. For example, when an application suddenly stops a process, it is obvious that the resources are exhausted, but by checking the code alone, you cannot clearly know where the resources are exhausted and why.

jstackIs a complete dump utility that returns a variety of threads running on the application. You can use it to identify problems.

Run with the vmid of the expected ProcessjstackA heap dump is generated. In this regard,jstackIt works the same way as pressing Ctrl-break in the console window. In the console window, Java processes are running or calling every
ThreadObjectThread.getAllStackTraces()OrThread.dumpStack().jstackThe call also dumps information about non-Java threads running in the VM. These threads act
ThreadObjects are not always available.

jstackOf-lParameters provide a long dump, including more details about the locks held by each Java thread, so it is extremely important to discover (and squash) deadlocks or scalability bugs.

Back to Top

4. jmap (sun. Tools. jmap)

Sometimes, the problem you are dealing with is an object leakage, such asArrayList(May hold thousands of objects) This release is not released. Another more common problem is that there seems to be an active garbage collection from a non-compressed extended heap.

When you try to find an object for leakage, it is useful to take a photo of the heap at a specified time and then review the content.jmapThe first part of this function is provided by taking snapshots of the heap. Then you can use
jhatThe utility analyzes heap data.

Like all other utilities described here, usejmapVery simple. SetjmapPoint to the vmid of the Java Process you want to take a snapshot of, and then give it some parameters to describe the generated result file. You want to pass
jmapThe options include the name of the dump file and whether to use a text file or binary file. Binary files are the most useful option, but it is not the best method to manually manipulate MB of bytes of text with hexadecimal values only when used in conjunction with an index tool.

Let's take a look at more information about the Java heap,jmapAlso supported-histo.-histoGenerate an object text bar chart, which is now referenced in a lot in the heap and is classified by the total number of bytes consumed by a specific type. It also provides the total number of examples for a specific type, supports partial original computation, and guesses the relative cost of each instance.

Unfortunately,jmapNo ImagejstatThe same period-and-max-count option,
jmap
(Orjmap.main()) It is relatively easy to periodically take snapshots by calling a shell script or loop of other classes. (In fact, this is join
jmapCan be used as the source patch of openjdk or as an extension of other utilities .)

Back to Top

5. jhat (COM. Sun. Tools. Hat. Main)

After dumping the heap to a binary file, you can usejhatAnalyze the binary heap dump file.jhatCreate an HTTP/html server that can be browsed in the browser. Provide a heap object-by-object view to freeze the server in time. It may be ridiculous to deal with the heap based on object references. You can get better services by performing some automatic analysis on the overall chaos. Fortunately,jhatSupports oql syntax for such analysis.

For exampleStringThe running oql query looks as follows:

select s from java.lang.String s where s.count >= 100

The result is displayed as the object link, and the complete content of the object is displayed. The field reference is another object that can be unbound from the reference. Oql queries can also call object methods, use regular expressions as part of queries, and use built-in query tools. A query tool,referrers()Function that displays all references to objects of the specified type. The following is a reference
FileObject Query:

select referrers(f) from java.io.File f 

You can find the complete oql syntax andjhatFeatures on the "oql help" page in the browser environment. SetjhatThe combination of oql and oql is an effective method for investigating objects in a heap with improper behaviors.

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.