About 5 things you don't know about Java performance Monitoring-part 2nd: Java process monitoring with JDK built-in analyzer
Fully functional built-in analyzers, such as Jconsole and VISUALVM, are sometimes more expensive than their performance costs-especially in systems running on production software. So, in the 2nd article focusing on Java performance monitoring, I'll look at 5 command-line profiling tools that allow developers to focus on one aspect of the running Java process.
The JDK includes a number of command-line utilities that can be used to monitor and manage Java application performance. Although most of these applications are labeled "experimental" and are technically unsupported, they are useful. Some are even seed materials for specific use tools that can be built using JVMTI or JDI.
1. JPS (SUN.TOOLS.JPS)
Many command-line tools require you to identify the Java processes that you want to monitor. This is not much different from the same tools that monitor local operating system processes and require a single program recognizer.
The "VMID" recognizer is not always the same as the local operating system process recognizer ("pid"), which is why we need the JDK JPS utility.
Using JPS in Java processes
As with most of the tools for configuring JDK and all the tools mentioned in this article, executable JPS is usually a thin wrapper around a Java class or a class set that performs most of the work. In Windows® environments, these tools are. exe files that use the JNI invocation API to invoke directly the classes mentioned above; in UNIX® environments, most tools are symbolic links to a shell script that starts a common launcher with the correct class name specified.
If you want to use the JPS (or any other tool) feature-ant script in a Java process-it is relatively easy to invoke main () only on each tool's "main" class. To simplify the reference, the class name appears in parentheses after each tool name.
The jps-name reflects the PS utility found on most UNIX systems-telling us the Jvmid to run the Java application. As the name suggests, JPS returns the VMID of all the discovered Java processes running on the specified machine. If JPS does not find a process, it does not mean that the Java process cannot be attached or studied, but that it does not advertise its usability.
If a Java process is found, JPS lists the command line that enabled it. This method of distinguishing Java processes is important because, as long as the operating system is involved, all Java processes are collectively referred to as "Java". In most cases, VMID is an important figure to note.
Starting with the parser
The easiest way to start with the analysis utility is to use a demo program like the one found in Demo/jfc/swingset2 SwingSet2. This avoids the possibility that the program will hang when it runs as a background/monitor program. When you understand the tools and their costs, you can try them in the actual program.
After loading the demo application, run JPS and pay attention to the returned VMID. To get better results, start the Java process with the-dcom.sun.management.jmxremote property set. If you do not use this setting, some of the data collected by some of the following tools may not be available.
2. Jstat (Sun.tools.jstat)
The Jstat utility can be used to collect a wide variety of statistical data. Jstat statistics are sorted into options, which are specified as the first parameter on the command line. For JDK 1.6来, you can run Jstat to view the list of available options by using the command-options. Some of the options are shown in Listing 1:
Listing 1. Jstat Options
-class
-compiler
-gc
-gccapacity
-gccause
-gcnew
-gcnewcapacity
-gcold
-gcoldcapacity
-gcpermcapacity
-gcutil
-printcompilation
The JDK record for the utility will tell you what is returned for each option in Listing 1, but most of them are for collecting garbage collectors or performance information for their parts. The-class option shows classes loaded and not loaded (making it an important utility for detecting ClassLoader leaks in the application server or code, and-compiler and-printcompilation display information about the Hotspot JIT compiler.)
By default, Jstat displays information when you check the information. If you want to take a snapshot at a certain time, specify the interval in milliseconds after the-options instruction. Jstat will continue to display a snapshot of the monitoring process information. If you want Jstat to make a specific number of snapshots before terminating, specify the number after the interval/time value.
If 5756 is the VMID that runs the SwingSet2 program a few minutes ago, the following command tells Jstat to perform a GC snapshot dump every 250 milliseconds for 10 lost generations, and then stop:
JSTAT-GC 5756 250 10
Please note that Sun (now Oracle) retains the right to change the output of various options and even the option itself without any prior notice. This is the disadvantage of using an unsupported utility. See Javadocs for details about each column in the Jstat output.