This article is too hardcore and is recommended for those with Java experience.

1. The introduction

Although we have covered various graphical JVM troubleshooting tools, there are many cases where we do not have a graphical operating environment to work with, so we need to use the command-line tools provided by the JDK.

2. JPS: tool for vm process status

JPS is by far the most frequently used JDK command-line tool. It lists running virtual machine processes and displays the names of their Main classes (Main Class, Main ()), as well as their local VM ids (LVMID, LocalVirtual Machine Identifier).

Command format:

jps [options ] [ hostid ] 
Copy the code

Example:

PS D:\> jps -l
5200 org.jetbrains.jps.cmdline.Launcher
16868 jdk.jcmd/sun.tools.jps.Jps
19368 org.jetbrains.idea.maven.server.RemoteMavenServer36
Copy the code

It can be seen that there are three JVM processes on this machine, 5200 and 19368 are the process of software IDEA, which can be seen from the class name, and there is another JVM process of JPS itself.

List some common parameters:

optional role
-l Output the full package name, application main class name, and the full path name of the JAR.
-q Output only VM identifiers, excluding className, JAR name, arguments in main method.
-m Output the parameters of main method.
-v Output JVM parameters.

3. Jstat: monitors VM statistics

Jstat (JVM Statistics Monitoring Tool) is a command line Tool used to monitor the running status of virtual machines.

The bin directory of the JDK is used to monitor the resource and performance of Java applications on the command line in real time, including Heap size and garbage collection.

The Jstat tool is particularly powerful, with numerous options to look in detail at how much each part of the heap is being used and how many classes are being loaded. To use this command, add the id of the process to view the process and the selected parameters. The reference format is as follows:

jstat -options 
Copy the code

You can list the options supported by the current JVM version. Common ones are:

  • -class (class loader)
  • -compiler (JIT)
  • – GC (GC heap state)
  • – GcCapacity (District size)
  • – GCCause (last GC stat and cause)
  • – GCNEW (New District Statistics)
  • – gcnewCapacity (New area size)
  • – Gcold (Statistics in Old Districts)
  • -gcoldcapacity (old area size)
  • – gcpermCapacity (Permanent region size)
  • – GCUTIL (GC Statistics Summary)
  • -printcompilation (HotSpot compilation statistics)

For example, if I want to view the GC summary of my local IDEA process, I can use the following command:

PS D:\> jstat -gcutil 5200
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT    CGC    CGCT     GCT
  0.00  43.29  20.83   0.08  97.05  93.67      1    0.003     0    0.000     -        -    0.003
Copy the code

The query results show that:

The Eden area of the new generation (E, indicating Eden) of my IDEA uses 20.83% of the space, and there are two Survivor areas (S0 and S1, indicating Survivor0 and Survivor1), and S0 is empty. While S1 uses 43.29%, the Old age (O, for Old) uses 0.08% of the space, and the Metaspace (M, for Metaspace) uses 97.05%.

Since the program was run, a total of one Minor GC (YGC, which stands for Young GC) occurred, with a total time of 0.003 seconds, and 0 Full GC (FGC, which stands for Full GC) occurred, with a total time of (FGCT, Full GC Time is 0 seconds, and the total GC Time (GCT, GC Time) is 0.003 seconds.

Jinfo: Java configuration information tool

Jinfo (Configuration Info for Java) can be used to view extended parameters of running Java applications, including Java System properties and JVM command-line parameters. You can also dynamically modify some of the running JVM parameters. When the system crashes, Jinfo can learn the configuration information of the crashed Java application from the core file.

You can use the -v parameter of the JPS command to view the list of parameters that are explicitly specified when a VM is started. However, if you want to know the default values of parameters that are not explicitly specified, you can only use the -flag option of jinfo in addition to searching for information.

Jinfo command format:

jinfo [option] pid
Copy the code

Query example:

PS D:\> jinfo -flags 5200
VM Flags:
-XX:CICompilerCount=4 -XX:InitialHeapSize=268435456 -XX:MaxHeapSize=734003200 -XX:MaxNewSize=244318208 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=89128960 -XX:OldSize=179306496 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
Copy the code

5. Jmap: Java Memory mapping tool

The jmap (Memory Map for Java) command is used to generate heapdump snapshots (commonly known as heapdump or dump files).

It can also view statistics for sample heap objects, view ClassLoader information, and finalizer queues.

Jmap command format:

jinfo [option] pid
Copy the code

Option Optional parameters:

  • No option: Displays the memory image information of the process, similar to the Solaris pmap command.
  • Heap: Displays Java heap details
  • Histo [:live] : Displays statistics of objects in the heap
  • Clstats: Prints classloader information
  • Finalizerinfo: Displays the objects in the F-Queue that are waiting for the Finalizer thread to execute the Finalizer method
  • Dump: : Generates a heap dump snapshot
  • F: When -dump does not respond, use -dump or -histo. In this mode, the live subparameter is invalid.
  • Help: Prints the help information
  • J: Specifies the parameters passed to the JVM running JMap
PS D:\> jmap -dump:format=b,file=idea.bin 5200
Dumping heap to D:\idea.bin ...
Heap dump file created
Copy the code

6. Jhat: vm heap dump snapshot analysis tool

The JDK provides the JHAT (JVM Heap Analysis Tool) command to be used in conjunction with Jmap to analyze Heap dump snapshots generated by JMap. Jhat has a mini-HTTP /Web server built in that generates the analysis results of the heap dump snapshots and allows you to view them in a browser.

Run the following command:

PS D:\> jhat idea.bin
Reading from idea.bin...
Dump file created Thu Oct 08 18:54:37 CST 2020
Snapshot read, resolving...
Resolving 147921 objects...
Chasing references, expect 29 dots.............................
Eliminating duplicate references.............................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
Copy the code

You can then visit http://localhost:7000/ in your browser to see the results of the analysis, but it’s a tool that few people use because it’s so rudimentary.

In contrast, we can analyze the dump files just produced by JMap using tools such as VisualVM or Eclipse Memory Analyzer or IBM HeapAnalyzer.

Jstack: Java stack trace tool

The jstack (Stack Trace for Java) command is used to generate a snapshot (threaddump or Javacore file) of vm threads at the current time.

Thread snapshot is the current virtual machine within each thread is executing method stack collection, generate thread snapshot is usually the purpose of locating the thread a long pause, such as deadlock between threads, infinite loop, request external resources lead to hang up for a long time and so on, are all lead to a common cause of thread long pause.

Jstack command format:

jstack [option] pid
Copy the code

Optional parameters for option:

  • -f: Forces the thread stack to be outputted when a normally outputted request is not answered.
  • -l: Displays additional information about the lock in addition to the stack.
  • -m: Displays the C/C++ stack if the local method is called.
PS D:\> jstack -l 5200
2020-10- 0819:03:39
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.221-b11 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x00000000037b8000 nid=0x3f20 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None
Copy the code

This article is constantly updated, you can search “Geek excavator” on wechat to read it in the first time, and the key words in reply have all kinds of tutorials PREPARED by me, welcome to read.