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 ").jps
The reason for the utility.
Use JPs in Java Process
Similar to configuring most JDK tools and all the tools mentioned in this articlejps
It 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 systemsps
Utility-tells us the jvmid for running the Java application. As the name suggests,jps
Returns the vmid of all detected Java processes running on the specified machine. If
jps
The 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,jps
The 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/SwingSet2
Swingset2 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 runjps
And pay attention to the returnedvmid
. To achieve better results, use-Dcom.sun.management.jmxremote
The 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)
jstat
Utilities can be used to collect a variety of different statistical data.jstat
Statistical 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
-options
Runjstat
View 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.-class
Option displays the loaded and unloaded classes (make it a test application server or code)
ClassLoader
Important utility leaked, and-compiler
And-printcompilation
Information about the hotspot JIT compiler is displayed.
By default,jstat
The information is displayed when you check the information. If you want to take a snapshot at intervals-options
Specify the interval in milliseconds after the command.jstat
Snapshots of monitoring process information are continuously displayed. If you want
jstat
Specify 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 telljstat
Execute a GC snapshot dump for 10 generations every 250 milliseconds, and then stop:
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.
jstat
Output 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.
jstack
Is 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 Processjstack
A heap dump is generated. In this regard,jstack
It works the same way as pressing Ctrl-break in the console window. In the console window, Java processes are running or calling every
Thread
ObjectThread.getAllStackTraces()
OrThread.dumpStack()
.jstack
The call also dumps information about non-Java threads running in the VM. These threads act
Thread
Objects are not always available.
jstack
Of-l
Parameters 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.jmap
The first part of this function is provided by taking snapshots of the heap. Then you can use
jhat
The utility analyzes heap data.
Like all other utilities described here, usejmap
Very simple. Setjmap
Point 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
jmap
The 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,jmap
Also supported-histo
.-histo
Generate 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,jmap
No Imagejstat
The 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
jmap
Can 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 usejhat
Analyze the binary heap dump file.jhat
Create 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,jhat
Supports oql syntax for such analysis.
For exampleString
The 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
File
Object Query:
select referrers(f) from java.io.File f |
You can find the complete oql syntax andjhat
Features on the "oql help" page in the browser environment. Setjhat
The combination of oql and oql is an effective method for investigating objects in a heap with improper behaviors.