Deep understanding of JVM - fault diagnosis tools

  • jps (JVM Process Status Tool): displays all HotSpot virtual machine processes in the specified system
  • jstat (JVM Statistics Monitoring Tool): users collect all aspects of the operation data of the HotSpot virtual machine
  • jinfo (Configuration Info for Java): view and adjust parameters of virtual machine in real time, and integrate into jhsdb in JDK9
  • jmap (Memory Map for java): generates memory dump snapshot of virtual machine, which is integrated into jhsdb in JDK9
  • jhat (JVM Heap Dump Browser): the user analyzes the heapdump file. It will establish an HTTP/HTML server, so that the user can view the analysis results on the browser. It is integrated into jhsdb in JDK9
  • jstack (Stack Trace for Java): generates a thread snapshot of the current time of the virtual machine, which is integrated into jhsdb in JDK9
  • jhsdb (Java hot sport debugger): process debugger, introduced in JDK9

jps

List the virtual machine processes running on the current machine, jps command format:

jps [option] [hostid]

-p: Only display VM label, not jar,class, main parameter and other information -m: Output the parameters passed in by the main function. The hello below is the parameters input from the command line when executing the program -l: Output the application main class full package name or jar full name -v: List jvm parameters

Execution example:

C:\Users\yuhao.wang3>jps -l
11824 sun.tools.jps.Jps
9860 org.jetbrains.idea.maven.server.RemoteMavenServer
7960

jstat

Is a command-line tool for monitoring various running status information of virtual machine. It can display running data such as class loading, memory, garbage collection, JIT compilation, etc. in the local or remote virtual machine process. It will be the preferred tool to locate the performance problems of virtual machine in the run-time on the server that only provides the plain text console environment without GUI Graphical interface.

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

Suppose that you need to query the process 9860 garbage collection status every 250 milliseconds for a total of 5 times, the command should be:

C:\Users\yuhao.wang3>jstat -gc 9860 250 5
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
10752.0 10752.0  0.0    0.0   64512.0   867.6    76800.0     8207.2   23808.0 22909.2 3072.0 2769.6      6    0.049   4      0.209    0.258
10752.0 10752.0  0.0    0.0   64512.0   867.6    76800.0     8207.2   23808.0 22909.2 3072.0 2769.6      6    0.049   4      0.209    0.258
10752.0 10752.0  0.0    0.0   64512.0   867.6    76800.0     8207.2   23808.0 22909.2 3072.0 2769.6      6    0.049   4      0.209    0.258
10752.0 10752.0  0.0    0.0   64512.0   867.6    76800.0     8207.2   23808.0 22909.2 3072.0 2769.6      6    0.049   4      0.209    0.258
10752.0 10752.0  0.0    0.0   64512.0   867.6    76800.0     8207.2   23808.0 22909.2 3072.0 2769.6      6    0.049   4      0.209    0.258
  • S0C: indicates the total size of S0 area
  • S0U: indicates the used size of S0 area

jinfo

To view and modify parameters of virtual machine, command format:

jinfo [option] pid

jinfo – sysprops can view the parameters obtained by System.getProperties() jinfo – the system default value of the parameter whose flag is not explicitly specified jinfo – flags displays the parameters of the virtual machine jinfo – flag + [parameter] can add parameters, but it is only found by java -XX:+PrintFlagsFinal – version and

Execution example:

C:\Users\yuhao.wang3>jinfo -flags 9860
Attaching to process ID 9860, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.112-b15
Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=264241152 -XX:MaxHeapSize=805306368 -XX:MaxNewSize=268435456 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88080384 -XX:OldSize=176160768 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
Command line:  -Djava.awt.headless=true -Didea.version==2018.3.2 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=GBK

jmap

Used to generate a heap dump snapshot (commonly referred to as a heapdump or dump file). Jmap is not only used to get dump files, but also to query the details of finalize execution queue, Java heap and permanent generation, such as space utilization, which collector is currently used, etc. Like the jinfo command, jmap has many functions that are limited on the Windows platform. In addition to the - dump option to generate the dump file and the - histo option to view the instance and space occupation statistics of each class, which are provided by all operating systems, other options can only be used under Linux/Solaris. Command format:

jmp [option] pid

To generate a dump file for the heap:

C:\Users\yuhao.wang3>jmap -dump:live,format=b,file=heap.bin 9860
Dumping heap to C:\Users\yuhao.wang3\heap.bin ...
Heap dump file created

jhat

Virtual machine heap dump snapshot analysis tool

Analyze the file you just generated:

C:\Users\yuhao.wang3>jhat C:\Users\yuhao.wang3\heap.bin
Reading from C:\Users\yuhao.wang3\heap.bin...
Dump file created Tue Jan 14 17:59:13 CST 2020
Snapshot read, resolving...
Resolving 151163 objects...
Chasing references, expect 30 dots..............................
Eliminating duplicate references..............................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

After the prompt of "Server is ready." is displayed on the screen, the user enters http://localhost:7000 / you can see the analysis results. Drag it down to the bottom. Main view"HeapHistogram".

jstack

The jstack (Stack Trace for Java) command is used to generate a thread snapshot of the current time of the virtual machine. Thread snapshot is the collection of the method stacks that each thread is executing in the virtual machine. The main purpose of generating thread snapshot is to locate the causes of thread pause for a long time, such as deadlock between threads, dead cycle, long wait caused by requesting external resources, etc., which are common causes of thread pause for a long time. Command format:

jstack [option] pid

-F: Force output thread stack when normal output request is not responded to -l: Show additional information about locks in addition to the stack -m: If you call a local method, you can display the stack of C/C + +

Example of query loop:

public class JstackDeadWhileTest {

    public static void main(String[] args) {
        int i = 1;
        while (++i > 0) {
            System.out.println(i);
        }
    }
}
C:\Users\yuhao.wang3>jps -l
18244 com.xiaolyuh.JstackDeadWhileTest
5444 org.jetbrains.jps.cmdline.Launcher
7960
10252 sun.tools.jps.Jps
17324 org.jetbrains.kotlin.daemon.KotlinCompileDaemon

C:\Users\yuhao.wang3>jstack -F 18244
Attaching to process ID 18244, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.112-b15
Deadlock Detection:

No deadlocks found.

Thread 14: (state = BLOCKED)


Thread 13: (state = BLOCKED)


Thread 12: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.lang.ref.ReferenceQueue.remove(long) @bci=59, line=143 (Interpreted frame)
 - java.lang.ref.ReferenceQueue.remove() @bci=2, line=164 (Interpreted frame)
 - java.lang.ref.Finalizer$FinalizerThread.run() @bci=36, line=209 (Interpreted frame)


Thread 11: (state = BLOCKED)
 - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
 - java.lang.Object.wait() @bci=2, line=502 (Interpreted frame)
 - java.lang.ref.Reference.tryHandlePending(boolean) @bci=54, line=191 (Interpreted frame)
 - java.lang.ref.Reference$ReferenceHandler.run() @bci=1, line=153 (Interpreted frame)


Thread 5: (state = IN_NATIVE)
 - java.io.FileOutputStream.writeBytes(byte[], int, int, boolean) @bci=0 (Compiled frame; information may be imprecise)
 - java.io.FileOutputStream.write(byte[], int, int) @bci=8, line=326 (Compiled frame)
 - java.io.BufferedOutputStream.flushBuffer() @bci=20, line=82 (Compiled frame)
 - java.io.BufferedOutputStream.flush() @bci=1, line=140 (Compiled frame)
 - java.io.PrintStream.write(byte[], int, int) @bci=30, line=482 (Compiled frame)
 - sun.nio.cs.StreamEncoder.writeBytes() @bci=120, line=221 (Compiled frame)
 - sun.nio.cs.StreamEncoder.implFlushBuffer() @bci=11, line=291 (Compiled frame)
 - sun.nio.cs.StreamEncoder.flushBuffer() @bci=15, line=104 (Compiled frame)
 - java.io.OutputStreamWriter.flushBuffer() @bci=4, line=185 (Compiled frame)
 - java.io.PrintStream.write(java.lang.String) @bci=27, line=527 (Compiled frame)
 - java.io.PrintStream.print(int) @bci=5, line=597 (Compiled frame)
 - java.io.PrintStream.println(int) @bci=6, line=736 (Compiled frame)
 - com.xiaolyuh.JstackDeadWhileTest.main(java.lang.String[]) @bci=13, line=14 (Compiled frame)

In the code, the getallstacktrace() method of java.lang.Thread class can be used to get the StackTraceElement object of all threads in the virtual machine. Using this method, you can complete most of the functions of jstack in a few lines of code. In the actual project, you can call this method to make an administrator page, and you can use the browser to view the thread stack at any time.

Performance monitoring and troubleshooting tools

VisualVM: multi close fault handling tool

Run the command JVM directly to open the VisualVM tool.

Plug-in installation

First go to this address to find the plug-in address of the corresponding version( https://xxx/updates.xml.gz): https://visualvm.github.io/pluginscenters.html

Tags: Programming Java snapshot jvm Maven

Posted on Tue, 14 Jan 2020 23:49:41 -0500 by komquat