In-depth understanding of Java Virtual Machines Part II (8)

Source: Internet
Author: User
Tags stack trace unique id visualvm

When we give a system a problem, the knowledge and experience are the key foundation, the data is the basis, the tool is the means of using the knowledge to process the data. The data here include: Run log, exception stack, GC log, thread snapshot (threaddump/javacore file), heap dump Snapshot (heapdump/hprof file), etc. Regular use of appropriate virtual machine monitoring and analysis tools can speed up our analysis of data and positioning to solve problems, but before we learn tools, we should also realize that the tool is always a layer of knowledge and skills packaging, there is no tool is a "secret weapon", learned to be able to package the medical ills.

Command-line tools for JDK

Java developers must know that the JDK's Bin directory has both "Java.exe" and "javac.exe" command-line tools, but not all programmers know the role of other command-line programs in the JDK's bin. The number and functionality of command-line tools in the Bin directory are always unknowingly increased and enhanced whenever the JDK is updated.

The author describes a subset of these tools, primarily for monitoring virtual machines and troubleshooting tools. These fault-handling tools are declared as "unsupported and experimental" products in the software's usage instructions, but in fact these tools are very stable and powerful to handle application performance issues, Play a big role in locating faults.

These tools are unusually small in size, mostly around 30KB. Not that the JDK development team deliberately crafted them so well as to dazzle them, but that most of these command-line tools are a thin package of the Jdk\lib\tools.jar class library. Their main function code is implemented in the Tools class library.

It is particularly helpful for the JDK team to choose to implement these monitoring tools in Java code: When an application is deployed to a production environment, either direct contact with the physical server or remote telnet to the server can be limited. With the interface inside the Tools.jar class library, we can implement powerful monitoring and analysis functions directly in the application.

The tools described in this chapter are all based on JDK 1.6 under the Windows platform, with different versions and operating systems.

Table Sun JDK monitoring and fault handling tools

Name Key Features
JPs JVM process Status Tool showing all hotspot virtual machine processes within a specified system
Jstat JVM Statistics minitoring Tool for collecting operational data for all aspects of the hotspot virtual machine
Jinfo Configuration info for Java, displaying virtual machine config information
Jmap Memory Map for Java, generating a ram dump Snapshot (heapdump) file for a virtual machine
Jhat The JVM Heap Dump Browser, used to parse the Heapdump file, establishes a http/html server that allows the user to view the results of the analysis on the browser
Jstack Stack Trace for Java, displaying a thread snapshot of a virtual machine
JPS: Virtual Machine Process Health tool

The names of many of the JDK's gadgets refer to how UNIX commands are named, and the JPS (JVM Process Status tool) is typical. In addition to the PS command named UNIX, the functionality is similar to PS: You can list running virtual machine processes and display the name of the virtual machine execution main class (the class where the main Class,main () function resides), and the unique ID of the local virtual machine for those processes (lvmid,local Virtual machine Identifier). Although the functionality is relatively single, it is the most frequently used JDK command-line tool, because most other JDK tools need to enter the Lvmid it queries to determine which virtual machine process to monitor. For on-premises virtual machine processes, the LVMID is consistent with the operating system's process ID (pid,process Identifier), and the Windows Task Manager or the UNIX PS command can also query the Lvmid of the virtual machine process. However, if more than one virtual machine process is used and cannot be located according to the process name, it is only possible to rely on the JPS command to display the main class's Ribbon in order to differentiate it.

JPS command format:

JPS [option] [HostID]

JPS Execution Example:

c:\users\administrator>jps-l5828 d:\work\develop\springtoolsuite\springsource\sts-3.3.0.release\\plugins/ org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar5680 Sun.tools.jps.Jps

JPS can query the status of the remote virtual machine process that opened the RMI service through the RMI protocol, HostID the host name registered in the RMI registry. Other common options for JPS are shown in the table below

Options Role
-Q Output only Lvmid, omit the name of the main class
-M Parameters of the main () function passed to the main class when the output virtual machine process starts
-L Output the full name of the main class, if the process is executing a jar package, the output jar path
-V JVM parameters when the output virtual machine process starts

Jstat: Virtual Machine Statistics monitoring tool

The Jstat (JVM Statistics monitoring tool) is a command-line utility for monitoring the various operational state information of a virtual machine. It can display the class load, memory, garbage collection, JIT compiling and other running data in the process of local or remote virtual machine, on the server without GUI image interface, only improve the plain text console environment, it will be the tool of choice for locating virtual machine performance problem at run time.

Jstat command format:

jstat [Option Vmid [Interval[s|ms] [count]]

The Vmid and Lvmid in the command format require special instructions: If the local virtual machine process, Vmid and lvmid are consistent, and if the remote virtual machine process, the VMID format should be:

[Protocol:] [//] lvmid [@hostname [:p Ort]/servername]

The parameters interval and count represent the query interval and number of times, and if these two parameters are omitted, the description is queried only once. Assuming that you need to query the process 5828 garbage collection every 250 milliseconds and query 5 times, the command line is as follows:

JSTAT-GC 5828 250 5

Option options represents the virtual machine information that the user wants to query, mainly divided into 3 categories: class load, garbage collection, and run-time compilation state, specific options and leases, see the following table:

Options Role
-class Monitor class load, unload quantity, total space, and time spent on class loading
-gc Monitor the Java heap status, including the Eden Zone, 2 survivor zones, the old age, and the capacity of a permanent generation
-gccapacity The monitoring content is basically the same as-GC, but the output focuses on the maximum and minimum space used by each area of the Java heap
-gcutil The monitoring content is basically the same as-GC, but the output focuses on the percentage of space used for the total space
-gccause Same as-gcutil function, but additional output causes the last GC to occur
-gcnew Monitor the status of the new generation GC
-gcnewcapacity The monitoring content is basically the same as the-gcnew, and the output focuses on the maximum and minimum space used
-gcold Monitoring the status of the GC in the old age
-gcoldcapacity The monitoring content is basically the same as the--gcold, and the output focuses on the maximum and minimum space used
-gcpermcapacity Maximum and minimum space used for output permanent generation
-compiler Output JIT compiler compiled methods, time-consuming and other information
-printcompilation How the output has been JIT-compiled

As an example:

C:\users\administrator>jstat-gcutil 5828 S0 S1 E O P ygc ygct FGC fgct GCT 0.00 0.00 1.82 52.18 99.91 329 4.894 269 80.244 85.139

This is where I monitor my eclipse's memory status. The results show that the new Generation Eden region (E, which represents Eden) uses 1.82% of the space, two survivor zones (S0, S1, Survivor0, Survivor1) inside are empty, the old age (O, for the older) and the permanent generation (P, Represents permanent) uses 52.18% and 99.91% of space respectively. Minor GC (Ygc,young GC) has occurred 329 times since the program was run, total time elapsed (Ygct,young GC) 4.894 seconds, full GC (FGC) 269 times, Total time consuming (FGCT) 80.244 seconds, all GC total time (GCT) 85.139 seconds.

Using the Jstat tool to monitor changes in the state of the virtual machine in plain text, it is not as straightforward as the visual monitoring tools that will be mentioned later, such as the VISUALVM, as shown in the chart. But many server administrators are accustomed to a common group in the text console oh, using the Jstat command directly in the console is already a common way to monitor.

Jinfo:java Configuration Information tool

The purpose of Jinfo (Configuration Info for Java) is to view and adjust the parameters of the virtual machine in real time. The-v parameter of the command that uses JPS can view the specified parameter list when the virtual machine starts, but if you want to know the system default value that is not displayed for the specified parameter, in addition to looking for information, You can only query using the-flag option of jinfo (if it is limited to JDK1.6 or later, it is also a good choice to use Java-xx:+printflagsfinal to see the default values of the parameters). Jinfo can also print the contents of the system.getproperties () of the virtual machine process using the-SYSPROPS option. This command was released during the JDK1.5 period with the Linux version of the JDK, which provided only the functionality of the information query, and after JDK1.6, Jinfo on both the Windows and Linux platforms, and added the ability to modify parameters at runtime, using-flag[+|-] Name or-flag Name=valule modifies part of the run-time writable virtual machine parameter values. JDK1.6, Jinfo still has a large limit on the functionality of the Windows platform, providing only the most basic-flag options.

Jinfo Command format:

jinfo [option] pid

Execute sample: Query the Cmsinitiatingoccupancyfraction parameter value.

C:\users\administrator>jinfo-flag cmsinitiatingoccupancyfraction 5828-xx:cmsinitiatingoccupancyfraction=-1

Jmap:java Memory Imaging Tool

The Jmap (Memory Map for Java) command is used to produce a heap dump snapshot (commonly known as a heapdump or dump file). If you do not use the Jmap command, there are some more "brute force" means to get a Java heap dump snapshot: The-xx:+heapdumponoutofmemoryerror parameter, for example, allows the virtual machine to generate a dump file automatically after an Oom exception occurs, by-xx:+ The Heapdumponctrlbreak parameter allows the virtual machine to generate a dump file using the [Ctrl]+[break] key, or the dump file can be "intimidated" by the Kill-3 command sending process exit signal under the Linux system.

Jmap's role is not just to get the dump file, it can also query the details of the Finalize execution queue, the Java heap, and the permanent generation, such as the space usage, the current collector, and so on.

As with the Jinfo command, Jmap has a number of features that are limited under the Windows platform, except that the-dump option to generate dump files and the-histo options for viewing each class's instance, the space occupancy statistics, and all operating systems are available only in linux/ Used under Solaris.

JMAP command format:

jmap [option] Vmid

Option options legal value with specific meaning:

Options Role
-dump Generates a Java heap dump snapshot. The format is:-dump:[live,]format=b,file=<filename>, where the live sub-argument indicates whether to dump only the surviving objects
-finalizerinfo Displays an object in F-queue that waits for the finalizer thread to execute the Finalize () method. Valid only on Linux/solaris platform
-heap Displays the Java heap details, such as which collector to use, parameter configuration, generational status, and so on. Valid only on Linux/solaris platform
-histo Displays the object statistics in the heap, including the class, number of instances, and total capacity
-permstat Displays the permanent memory status with ClassLoader as the statistical aperture. Valid only on Linux/solaris platform
-F Use this option to force a dump snapshot when the virtual machine process does not respond to the-dump option. Valid only on Linux/solaris platform

Examples:

C:\users\administrator>jmap-dump:format=b,file=eclipse.bin 5828Dumping Heap to C:\Users\Administrator\ Eclipse.bin ... Heap dump file created

This is an example of a dump snapshot file that uses Jmap to generate a running eclipse, and 5828 is the lvmid that JPS queries.

Jhat: Virtual machine heap dump snapshot analysis tool

The

Sun JDK provides the jhat (JVM heap Analysis Tool) command to use with Jmap to analyze jmap generated heap dump snapshots. Jhat built a miniature http/html server, generate dump file analysis results, can be viewed in the browser, but realistically speaking, in the actual work, unless there is really no other tool available, otherwise will not go directly to use the Jhat command to analyze the Demp file, There are two main reasons: consciousness typically does not parse the dump file directly on the server where the application is deployed, even if it is possible to copy the dump file to other machines for analysis, since the process of analyzing the work is time consuming and consumes hardware resources, since it is done on other machines, There is no need to receive command-line tool restrictions. Another reason is that Jhat's analytical capabilities are relatively rudimentary, and VISUALVM and tools such as Eclipse Memory Analyzer and IBM Heapanalyzer, which specialize in analyzing dump files, enable more powerful and professional analysis capabilities than jhat.

C:\users\administrator>jhat eclipse.binreading from eclipse.bin ... Dump file created tue aug 19 16:01:10 cst 2014snapshot read,  resolving ... Resolving 2126401 objects ... Chasing references, expect 425 dots ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... ............ Eliminating duplicate references ......................................................................................................................... ......................................................................................................................... ..................................................................................................................................... Snapshot resolved. Started http server on port 7000server is ready.

"Server is ready." After it appears, you can type http://localhost:7000 in the browser to view the results of the analysis.

The analysis results are grouped by default in packages, and the analysis of memory leaks mainly uses the functionality of the "Heap histogram" (like the Jmap-hosto function) and the OQL tab. The former can find the object with the largest total memory capacity, which is a standard object query statement that uses SQL-like syntax to query the objects in memory.

Jstack:java Stack Trace Tool

The Jstack (Stack Trace for Java) command is used to generate a thread snapshot of the current moment of the virtual machine (commonly referred to as a threaddump or Javacore file). A thread snapshot is a collection of method stacks that each thread in the current virtual machine is executing, and the primary purpose of generating the thread snapshot is to locate the cause of a long pause on threads, such as inter-thread deadlocks, dead loops, long waits that are caused by requests for external resources, and so on, which are common causes of long-running threads. When a thread pauses, you can see the call stack of each thread by Jstack, and you know what the unresponsive thread is doing in the background, or what resources are waiting for it.

Jstack command format:

jstack [option] Vmid

The legal value of option options is as follows:

Options Role
-F Forces the output thread stack when a normal output request is not responded to
-L Displays additional information about the lock, in addition to the stack
-M If you call the local method, you can display the C + + stack

Instance (view the eclipse thread stack with Jstack):

C:\users\administrator>jstack-l 5828

The result of the operation is too long to intercept.

In JDK1.5, the Java.lang.Thread class adds a new Getallstacktraces () method to get the Stacktraceelement object for all threads in the virtual machine. Using this method can be a few simple lines of code to complete the jstack of most of the functions, in the actual project may call this method as an administrator page, you can use the browser at any time to view the thread stack, the code is as follows:

<%@ page import= "java.util.Map"%>



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.