[In-depth understanding of java virtual machine] Introduction to JVM fault handling tools

foreword

Everyone knows that there are command-line tools such as java.exe and javac.exe in the bin directory of the JDK. These tools are mainly used to monitor the running status of the virtual machine and perform troubleshooting. These troubleshooting tools are not simply given to JDK users as a "gift" by Oracle Corporation. According to the software availability and authorization, they can be divided into three categories:

  • Commercial authorization tools: mainly JMC (Java Mission Control) and the JFR (Java Flight Recorder) it will use. JMC, an operation and maintenance monitoring suite originally from JRockit, has been integrated into OracleJDK since JDK 7 Update 40.
    JDK11 Previously there was no need for a separate download, but there was a fee to use it in a commercial environment.

  • Formally supported tools: This type of tool is a long-term supported tool. There may be slight differences in such tools between different platforms and different versions of JDK, but there will be no sudden disappearance of a certain tool.

  • Experimental tools: This type of tools is declared in their instructions as "unsupported and experimental" (Unsupported and Experimental) products, which may become regular in the future, or may be silent in a certain JDK version Disappeared without a trace. But in fact, they are usually very stable and powerful, and can also play a big role in dealing with application performance problems and locating faults.

1. jps: virtual machine process status tool

jps (JVM Process Status Tool) function: it can list the running virtual machine processes, and display the name of the virtual machine execution main class (Main Class, the class where the main() function is located) and the local virtual machine unique ID (LVMID) of these processes , Local Virtual Machine Identifier).

Command format:

jps [ options ] [ hostid ]

Example:

jps
1764 RemoteMavenServer
18516 Launcher
17868 Jps

options:

options describe
-q Only output the process ID, ignoring the main class information
-l Output the full name of the main class, or output the path when executing the JAR package
-m Output the parameters passed to the main class main() function when the virtual machine process starts
-in Output the JVM parameters when the virtual machine process starts

1. Output remote machine information

jps can also query the process status of the remote virtual machine with the RMI service enabled through the RMI protocol. The parameter hostid is the host name registered in the RMI registry.

The jps link remotely outputs JVM information, and RMI needs to be registered, otherwise an error will be reported:

jps -lv 127.0.0.1
RMI Registry not available at 127.0.0.1:1099
Connection refused to host: 127.0.0.1; nested exception is:
        java.net.ConnectException: Connection refused: connect

1. Register RMI and start jstatd C:\Program Files\Java\jdk1.8.0_91\binAdd a file named jstatd.all.policy in your directory.

The content of the jstatd.all.policy file is as follows:

grant codebase "file:${java.home}/../lib/tools.jar" {
    
    
   permission java.security.AllPermission;
};

2. After adding the configuration file, register the added jstatd.all.policy file in the bin directory:

C:\Program Files\Java\jdk1.8.0_91\bin>jstatd -J-Djava.security.policy=jstatd.all.policy

A successful example is as follows:

C:\Users\Administrator>jps -lv 127.0.0.1
12708 sun.tools.jps.Jps -Denv.class.path=.;G:\Java\jdk1.8.0_91\lib\dt.jar;G:\Java\jdk1.8.0_91\lib\tools.jar; -Dapplication.home=C:\Program Files\Java\jdk1.8.0_91 -Xms8m
16116  exit -Xms128m -Xmx3096m -XX:ReservedCodeCacheSize=512m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=100 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -XX:+HeapDumpOnOutOfMemoryError
 -Didea.vendor.name=JetBrains -Didea.paths.selector=IntelliJIdea2021.1 -XX:ErrorFile=C:\Users\Administrator\java_error_in_idea64_%p.log -XX:HeapDumpPath=C:\Users\Administrator\java_error_in_idea64.hprof
1764 org.jetbrains.idea.maven.server.RemoteMavenServer -Djava.awt.headless=true -Dmaven.defaultProjectBuilder.disableGlobalModelCache=true -Didea.version=2021.1.2 -Didea.maven.embedder.version=3.1.1 -Xmx768m -Dfile.e
18516 org.jetbrains.jps.cmdline.Launcher -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs="" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=G:/ideawork -Dpreload.config.path=G:/IntelliJ IDEA 2021.1.
net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=1570513135976058670 -Dfile.encoding=GBK -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2021.1 -Didea.home.path=G:\IntelliJ IDEA 2021.1
a2021.1/.IntelliJIdea/system/log/build-log -Djps.fallback.jdk.hom
2684 sun.tools.jstatd.Jstatd -Denv.class.path=.;G:\Java\jdk1.8.0_91\lib\dt.jar;G:\Java\jdk1.8.0_91\lib\tools.jar; -Dapplication.home=C:\Program Files\Java\jdk1.8.0_91 -Xms8m -Djava.security.policy=jstatd.all.policy

Parameter combination:

jps -lm 127.0.0.1

2. jstat: virtual machine statistics monitoring tool

jstat (JVM Statistics Monitoring Tool), used to monitor various running status information of the virtual machine. It can view runtime data such as class loading, memory, garbage collection, and just-in-time compilation in local or remote virtual machine processes.

Command format:

jstat [ option vmid [interval[s|ms] [count]] ]

vmid: If you are viewing a remote machine, you need to follow this format:[protocol:][//]lvmid[@hostname[:port]/servername]

interval and count, indicating the query interval and times, for example, query the gc collection status of the process ID every 1000 milliseconds, and query 5 times each time.jstat -gc 111552 1000 3

The options are mainly divided into three categories: class loading, garbage collection, and runtime compilation status:

options describe
-class Monitor class loads, number of unloads, total space, and how long class loads take
-gc Monitor the Java heap situation, including Eden area, 2 Survivor areas, old generation, permanent generation or jdk1.8 yuan space, capacity, used space, total garbage collection time, etc.
-gccapacity The monitoring content is basically the same as -gc, but the output mainly focuses on the maximum and minimum space used by each area of ​​the Java heap
-gcutile The monitoring content is basically the same as -gc, but the output mainly focuses on the percentage of used space to total space
-gccause Same function as -gcutil, but will additionally output the cause of the last garbage collection
-gcnew Monitor young generation garbage collection
-gcnewcapacity The monitoring content is basically the same as -gcnew, and the output mainly focuses on the maximum and minimum space used
-gcold Monitor Old Generation Garbage Collection
-gcoldcapacity The monitoring content is basically the same as -gcold, and the output mainly focuses on the maximum and minimum space used
-compiler Output the method compiled by the real-time compiler, time-consuming and other information
-printcompilation Output methods that have been just-in-time compiled
-gcpermcapacity jdk1.7 and below, permanent generation space statistics
-gcmetacapacity jdk1.8, metaspace statistics

Example:

C:\Users\Administrator>jstat -class 14944 1000 5
Loaded  Bytes  Unloaded  Bytes     Time
  6216 11190.6        1     0.9       4.70
  6216 11190.6        1     0.9       4.70
  6216 11190.6        1     0.9       4.70

C:\Users\Administrator>jstat -compiler 14944 1000 5
Compiled Failed Invalid   Time   FailedType FailedMethod
    3290      0       0     7.73          0
    3290      0       0     7.73          0
C:\Users\Administrator>jstat -gcutil 14944 1000 5
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT
  0.00  34.71  89.33   6.49  94.64  91.92      5    0.080     1    0.048    0.128
  0.00  34.71  89.33   6.49  94.64  91.92      5    0.080     1    0.048    0.128


C:\Users\Administrator>jstat -gc 14944 1000 5
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC
6656.0 10752.0  0.0   3732.1 131072.0 117082.2  82944.0     5384.1   28416.0 26893.9 3840.0 3529.8      5    0.080   1

3. jinfo: Java configuration information tool

The function of jinfo (Configuration Info for Java) is to view and adjust various parameters of the virtual machine in real time.

Command format:

jinfo [ option ] pid

Example:

C:\Users\Administrator>jinfo -flag MetaspaceSize 14944
-XX:MetaspaceSize=21807104

4. jmap: Java memory mapping tool

The jmap (Memory Map for Java) command is used to generate a heap dump snapshot (generally called heapdump or dump file). If you do not use the jmap command, there are still some "violent" means to obtain a Java heap dump snapshot: for example -XX: +HeapDumpOnOutOfMemoryError parameter, which allows the virtual machine to automatically generate a heap dump snapshot file after a memory overflow exception occurs , through the -XX: +HeapDumpOnCtrlBreak parameter, you can use the [Ctrl]+[Break] key to let the virtual machine generate a heap dump snapshot file, or send a process exit signal through the Kill-3 command under the Linux system to "scare" the virtual machine , can also successfully get the heap dump snapshot.

The role of jmap is not only to obtain heap dump snapshots, but also to query the detailed information of finalize execution queue, Java heap and method area, such as space usage, which collector is currently used, etc.

Command format:

jmap [ option ] pid

option: option parameter

pid: ID of the process that needs to print configuration information

executable: the Java executable that generates the core dump

core: the core file that needs to print configuration information

server-id: optional unique id, if multiple debugging servers are running on the same remote host, use this option parameter to identify the server

remote server IP or hostname: IP address or hostname of the remote debugging server

options:

options describe
-dump Generate a Java heap dump snapshot.
-finalizerinfo Displays the objects waiting for the Finalizer thread to execute the finalize method in the F-Queue. Linux platform
-heap Displays detailed information about the Java heap, such as: which collector is used, parameter configuration, and generational status. Linux platform
-histo Display object statistics in the heap, including class, number of instances, and total capacity
-permstat Display permanent generation memory status, jdk1.7, permanent generation
-F When the virtual machine process is not responsive to the -dump option, a snapshot can be forced. Linux platform

Example heap dump file:

jmap -dump:live,format=b,file=heap.bin 14944
C:\Users\Administrator>jmap -dump:format=b,file=idea.bin 14944
Dumping heap to C:\Users\Administrator\idea.bin ...
Heap dump file created

Print loading class example:

C:\Users\Administrator>jmap -clstats 14944
Attaching to process ID 14944, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.91-b15
finding class loader instances ..done.
computing per loader stat ..done.
please wait.. computing liveness.......liveness analysis may be inaccurate ...
class_loader    classes bytes   parent_loader   alive?  type

<bootstrap>     1698    2983149   null          live    <internal>
0x00000007736a41e8      1       880     0x00000006c1c14038      dead    sun/reflect/DelegatingClassL

5. jhat: virtual machine heap dump snapshot analysis tool

JDK provides the jhat (JVM Heap Analysis Tool) command to be used with jmap to analyze the heap dump snapshot generated by jmap. jhat has a built-in tiny HTTP/Web server, and after generating the analysis results of heap dump snapshots, they can be viewed in the browser.

However, in actual work, unless there are really no other tools available, most people will not directly use the jhat command to analyze the heap dump snapshot file. There are two main reasons:

  • One is that the heap dump snapshot is generally not directly analyzed on the server where the application is deployed. Even if it can be done, the heap dump snapshot file will be copied to other machines for analysis as much as possible, because the analysis work is time-consuming and extremely Since the process of consuming hardware resources must be carried out on other machines, there is no need to be limited by command line tools.

  • Another reason is that the analysis function of jhat is relatively simple. VisualVM, which will be introduced later, and tools such as Eclipse Memory Analyzer and IBM HeapAnalyzer, which are professionally used to analyze heap dump snapshot files, can be more powerful than jhat. Professional analysis function. The following code list demonstrates the use of jhat to analyze the memory snapshot file of Eclipse IDE generated by jmap in the previous section.

Example:

C:\Users\Administrator>jhat idea.bin
Reading from idea.bin...
Dump file created Wed Oct 26 20:48:34 CST 2022
Snapshot read, resolving...
Resolving 1394404 objects...
Chasing references, expect 278 dots...................................................................................
Eliminating duplicate references......................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

After the prompt "Server is ready." is displayed on the screen, the user can enter http://localhost:7000/ in the browser to see the analysis results, as shown in the figure.
insert image description hereinsert image description here

The analysis results are grouped and displayed in units of packages by default. The analysis of memory leaks will mainly use the "Heap Histogram" (same as jmap-histo function) and the function of the OQL tab. The former can find the object with the largest total capacity in the memory , the latter is a standard object query language that uses SQL-like syntax to query and count objects in memory.

Six, jstack: Java stack trace tool

The jstack (Stack Trace for Java) command is used to generate a thread snapshot (generally called a threaddump or javacore file) of the virtual machine at the current moment. A thread snapshot is a collection of method stacks being executed by each thread in the current virtual machine. The purpose of generating a thread snapshot is usually to locate the cause of a long pause in a thread, such as a deadlock between threads, an infinite loop, or a long time caused by requesting external resources. Hanging, etc., are common causes of thread pauses for a long time. When the thread pauses, check the call stack of each thread through jstack, and you can know what the unresponsive thread is doing in the background, or what resources it is waiting for.

Command format:

jstack [ option ] vmid

options:

options describe
-F When the request for normal output is not responded, force output thread stack
-l Show additional information about locks in addition to the stack
-m If the native method is called, the c/c++ stack can be displayed

Example:

jstack -l 14944

From JDK 5 onwards, the java.lang.Thread class adds a getAllStackTraces() method to obtain the StackTraceElement objects of all threads in the virtual machine. Using this method, most functions of jstack can be completed with a few simple lines of code. In actual projects, you may wish to call this method to create an administrator page. You can use a browser to view the thread stack at any time, as shown in the following code list.

<%@ page import="java.util.Map"%>
<html>
<head>
<title>服务器线程信息</title>
</head>
<body>
<pre>
<%
	for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStack-Traces().entrySet()) {
    
    
		Thread thread = (Thread) stackTrace.getKey();
		StackTraceElement[] stack = (StackTraceElement[]) stackTrace.getValue();
		if (thread.equals(Thread.currentThread())) {
    
    
		continue;
		}
		out.print("\n线程: " + thread.getName() + "\n");
		for (StackTraceElement element : stack) {
    
    
		out.print("\t"+element+"\n");
		}
	}
%>
</pre>
</body>
</html>

7. Summary of basic tools

The following table lists all the tools that come with the JDK (including those that used to exist but have been removed in the latest version) and their brief uses. In the higher version of the JDK, most of these tools have more powerful substitutes. For example, the command line mode of JCMD and JHSDB, but the usage method is similar. No matter what version of JDK has been developed, learning these basic tool commands will not be outdated and wasteful.

  • Basic tools: used to support basic program creation and operation.

    insert image description here

  • Security: Used for program signing, setting up security tests, etc.
    insert image description here

  • Internationalization: Used to create local language files.

    insert image description here

  • Remote Method Invocation: For service interaction across the web or network.
    insert image description here

Java IDL and RMI-IIOP: After more than ten years of CORBA support ended in JDK 11, these tools are no longer provided.
insert image description here

  • Deployment tools: used for program packaging, publishing and deployment.
    insert image description here

  • Java Web Start。
    insert image description here

  • Performance monitoring and troubleshooting: used to monitor and analyze the running information of the Java virtual machine and troubleshoot problems.
    insert image description here

  • WebService Tools: Removed in JDK 11 along with CORBA.
    insert image description here

  • REPL and scripting tools.
    insert image description here

insert image description here
Like, collect
and follow

Guess you like

Origin blog.csdn.net/qq_35764295/article/details/127538540