Deep Understanding of Java Virtual Machines (IV)

Keywords: Java jvm snapshot JDK

Detailed Description of Virtual Machine Performance Monitoring and Fault Handling Tools

4.1 Overview

This article refers to Chapter IV of Zhou Zhiming's "Deep Understanding of Java Virtual Machine", in order to sort out ideas, simply record it for later reference.

JDK itself provides a lot of convenient monitoring tools for JVM performance tuning. In addition to integrated Visual VM and jConsole, there are also small tools such as jps, jstack, jmap, jhat, jstat, hprof and so on. This article hopes to be a useful tool for JVM performance tuning, so that you can begin to understand the common tools for JVM performance tuning.

4.2 JDK Command Line Tools

Command Name Full name purpose
jstat JVM Statistics Monitoring Tool Used to collect running data of various aspects of Hotspot Virtual Machine
jps JVM Process Status Tool Displays all HotSpot virtual machine processes in the specified system
jinfo Configuration Info for Java Display virtual machine configuration information
jmap JVM Memory Map Generate memory dump snapshots of virtual machines and heapdump files
jhat JVM Heap Dump Browser Used to analyze heapdump files, it establishes an HTTP/HTML server that allows users to view analysis results on browsers.
jstack JVM Stack Trace Display thread snapshots of virtual machines

4.2.1 jps: Virtual Machine Process Status Tool

jps [options] [hostid]

- l: Output main class full name or jar path
- q: Output LVMID only
- m: Output parameters passed to main() at JVM startup
- v: Display the specified JVM parameters when the output JVM starts

$ jps -l
16256 sun.tools.jps.Jps
14904 org.jetbrains.jps.cmdline.Launcher
15016 com.ecej.esmart.gateway.service.impl.PengleiTest
  • Function: It can list the running virtual machine processes, and the main class name and the unique ID (LVMID) of the local virtual machine executed by the online virtual machine.
  • For local virtual machines, LVMID is identical to the process ID of the operating system.
  • Other tools usually need to rely on jps to obtain LVMID.
  • Main options: - q (output LVMID only), - m (output parameters to main function), - l (output full name of main class), - v (output virtual machine start JVM parameters);

4.2.2 jstat: Virtual Machine Statistical Information Monitoring Tool

jstat(JVM statistics Monitoring) is a command used to monitor the running state information of the virtual machine. It can display the class loading, memory, garbage collection, JIT compilation and other running data in the process of the virtual machine.

Command format

$ jstat [option] LVMID [interval] [count]

parameter

  • [option]: Operational parameters
  • LVMID: Local Virtual Machine Process ID
  • [interval]: Time interval for continuous output
  • [count]: Number of consecutive outputs

Overview of option parameters

Selection To be used as _____________
-class Monitor class loading, unloading number, total space, and class loading time
-gc Monitor Java heap status, including Eden area, two Survivor areas, capacity, used space, GC time aggregation, etc.
-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 mainly on the percentage of the total space used.
-gccause Like the - gcutil function, the extra output causes the last GC to occur
-gcnew Monitoring Cenozoic GC
-gcnewcapacity The monitoring content is basically the same as - gcnew, but the output is mainly concerned with the maximum and minimum space used.
-gcold Monitoring the status of GC in the elderly
-gcoldcapacity The monitoring content is basically the same as - gcold, but the output is mainly concerned with the maximum and minimum space used.
-gcpermcapacity Maximum and Minimum Space Used by Output Permanent Generation
-compiler Output JIT compiler compiled methods, time-consuming and other information
-printcompilation Method of output compiled by JIT

Detailed explanation of option parameters

-class

Monitor class loading, unloading number, total space, and time consumed

$ jstat -class 14988
Loaded  Bytes  Unloaded  Bytes     Time
   577  1152.8        0     0.0       0.17
  • Loaded: Number of class es loaded
  • Bytes: class byte size
  • Unloaded: Number of unloaded class es
  • Bytes: Bytes size of unloaded class es
  • Time: Load time

-compiler

Output JIT compiled method number time-consuming, etc.

$ jps -l -m
8657 org.apache.catalina.startup.Bootstrap start
12706 com.ecej.cust.service.run.Startup 60001
52132 sun.tools.jps.Jps -l -m
55671 customer.jar --server.port=8082 --log.home=/data/dubbo/logs/customer --log.level=info --log.stdout=0 ecej.ops.iswitch=1 --ecej.ops.port=80 --jmx.rmi.port=9991

$ jstat -compiler 55671
Compiled Failed Invalid   Time   FailedType FailedMethod
   13784      1       0   102.02          1 com/mysql/jdbc/AbandonedConnectionCleanupThread run
  • Compiled: Number of Compiles
  • Failed: Number of compilation failures
  • Invalid: Invalid number
  • Time: Compilation time-consuming
  • Failed Type: Failed Type
  • Failed Method: Fully qualified name of the failed method

-gc

Behavior statistics of garbage collection heaps, common commands

$ jstat -gc 55671
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
512.0  512.0   0.0    0.0   1285632.0 56336.6   259584.0   61064.8   74416.0 72873.0 8368.0 8015.0    114    0.854  103    14.911   15.765

C is the total capacity of Capacity, U is the capacity used by Used.

  • S0C: Total capacity of survivor 0 zone
  • S1C: Total capacity of survivor 1
  • S0U: Capacity already in use in survivor 0
  • S1C: Capacity used in survivor1 area
  • EC: Total capacity of Eden area
  • EU: Capacity used in Eden area
  • OC: Total capacity of Old area
  • OU: Old area used capacity
  • Current perm Capacity (KB) of PC
  • Use of PU perm (KB)
  • YGC: New Generation Waste Recycling Number
  • YGCT: New Generation Garbage Recycling Time
  • FGC: Number of garbage recycling in the elderly
  • FGCT: Older Garbage Recycling Time
  • GCT: Total Waste Recovery Time
$ jstat -gc 55671 2000 20

This command means output 1262 gc cases every 2000 ms, a total of 20 times

-gccapacity

Same as - gc, but it also outputs the maximum and minimum space used by each area of the Java heap

$ jstat -gccapacity 55671
 NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC 
 84992.0 1360384.0 1287168.0  512.0  512.0 1285632.0   171008.0  2721280.0   259584.0   259584.0      0.0 1116160.0  74416.0      0.0 1048576.0   8368.0    114   103
  • NGCMN: The Minimum Space Occupied by the Cenozoic Era
  • NGCMX: The largest space occupied by the Cenozoic
  • OGCMN: The Minimum Space Occupied by Older Generations
  • OGCMX: The Largest Space Occupied by Older Generations
  • OGC: Current capacity of older generations (KB)
  • OC: Space for the Aged (KB)
  • PGCMN: Minimum space occupied by perm
  • PGCMX: Maximum space occupied by perm

-gcutil

Same as - gc, but the output is the percentage of used space in total space

$ jstat -gcutil 55671
  S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.00   0.00   6.25  23.52  97.93  95.78    114    0.854   103   14.911   15.765
  • LGCC: Reasons for Recent Waste Recycling
  • GCC: Reasons for Current Garbage Recycling

-gcnew

Statistical Cenozoic Behavior

$ jstat -gcnew 55671
 S0C    S1C    S0U    S1U   TT MTT  DSS      EC       EU     YGC     YGCT  
 512.0  512.0    0.0    0.0 15  15  512.0 1285632.0 100094.5    114    0.854
  • TT: Tenuring threshold
  • MTT: Maximum tenuring threshold
  • DSS: survivor region size (KB)

-gcnewcapacity

Statistics of Cenozoic and its corresponding memory space

$ jstat -gcnewcapacity  55671
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC      YGC   FGC 
   84992.0  1360384.0  1287168.0 453120.0    512.0 453120.0    512.0  1359360.0  1285632.0   114   103
  • NGC: Current Young Generation Capacity (KB)
  • S0CMX: Maximum S0 Space (KB)
  • S0C: Current S0 Space (KB)
  • ECMX: Maximum eden space (KB)
  • EC: Current eden space (KB)

-gcold

Statistical Behavior of the Old Generation

$  jstat -gcold 55671
   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
 74416.0  72873.0   8368.0   8015.0    259584.0     61064.8    114   103   14.911   15.765

-gcoldcapacity

Statistics of the size and space of the Paleozoic

$ jstat -gcoldcapacity 55671
   OGCMN       OGCMX        OGC         OC       YGC   FGC    FGCT     GCT   
   171008.0   2721280.0    259584.0    259584.0   116   105   15.170   16.033

-gcpermcapacity

Eternal Behavior Statistics

Delete Permanent Generation in JDK 8
Where is JDK 8 Java Permanent Generation
Part of the content of the permanent code in Hotspot moves to the Java heap and the rest to local memory.

$ jstat -gcpermcapacity 28920
    PGCMN      PGCMX       PGC         PC      YGC   FGC    FGCT     GCT   
 1048576.0  2097152.0  1048576.0  1048576.0     4     0    0.000    0.242

-printcompilation

hotspot compilation statistics

$ jstat -printcompilation 55671
Compiled  Size  Type Method
   13785    123    1 java/io/DataOutputStream write
  • Compiled: Number of compiled tasks performed
  • Size: The number of bytes in the method bytecode
  • Type: Compile type
  • Method: The class name and method name of the compiled method. The class name uses "/" instead of "." as the space separator. The method name is the method name given to the class. The format is consistent with the HotSpot-XX:+PrintComplation option.

4.2.3 jinfo: Java Configuration Information Tool

The command jinfo(JVM Configuration info) is used to view and adjust virtual machine running parameters in real time.
Previous jps-v passwords can only view the specified parameters. If you want to see the values of the parameters that are not displayed, you need to use the jinfo password.

Command format

jinfo [option] [args] LVMID

option parameter

  • - flag: Output the value of the specified args parameter
  • - flags: Output all JVM parameters without args parameters
  • - sysprops: Output system properties, equivalent to System.getProperties()

$ jinfo -flags 55671
Attaching to process ID 55671, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.65-b01
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=262144000 -XX:MaxHeapSize=4179623936 -XX:MaxNewSize=1393033216 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=87031808 -XX:OldSize=175112192 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC 
Command line:  

4.2.4 jmap: Java Memory Mapping Tool

  • Function: Used to generate heap dump snapshots (commonly referred to as heapdump or dump files)
  • Other ways to generate heapdump: using parameter - XX:+HeapDumpOnOutOfMemoryError using parameter - XX:+HeapDumpOnCtrlBreak and then using Ctrl+Break; Linux system using kill-3 generation
  • It can also query finalize execution queues, Java heaps, and permanents for details.

Command format

jmap [option] LVMID

option parameter

  • Dump: Generate heap dump snapshots
  • finalizerinfo: Objects displayed in the F-Queue queue waiting for Finalizer threads to execute finalizer methods
  • Heap: Display Java heap details
  • His: Displays statistical information about objects in the heap
  • permstat : to print permanent generation statistics
  • F: Force dump snapshots when - dump does not respond

-dump
Common formats

-dump::live,format=b,file=<filename> pid 

dump heaps into files, format specifies the output format, live specifies the live object, and file specifies the file name.

$ jmap -dump:live,format=b,file=penglei.hprof 55671
Dumping heap to /home/publish/penglei.hprof ...
Heap dump file created

The suffix penglei.hprof is for subsequent openings directly using MAT(Memory Anlysis Tool).

-finalizerinfo

Print information about objects waiting to be recycled

$ jmap -finalizerinfo 55671
Attaching to process ID 55671, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.65-b01
Number of objects pending for finalization: 0

You can see that there are no objects in the current F-QUEUE queue waiting for Finalizer threads to execute finalizer methods.

-heap

Print the summary information of heap, the algorithm used by GC, the configuration of heap and the usage of wise heap, which can be used to judge the current usage of memory and garbage collection.

$ jmap -heap 55671
Attaching to process ID 55671, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.65-b01

using thread-local object allocation.
Parallel GC with 8 thread(s)//GC mode 

Heap Configuration://Heap memory initialization configuration
   MinHeapFreeRatio         = 0 //Set the JVM heap minimum idle ratio (default 40) corresponding to the JVM startup parameter - XX:MinHeapFreeRatio
   MaxHeapFreeRatio         = 100 //Set the JVM heap maximum idle ratio (default 70) corresponding to the JVM startup parameter - XX:MaxHeapFreeRatio
   MaxHeapSize              = 4179623936 (3986.0MB)//The corresponding JVM startup parameter - XX:MaxHeapSize = Sets the maximum size of the JVM heap
   NewSize                  = 87031808 (83.0MB)//Corresponding to the JVM startup parameter - XX:NewSize = Set the default size of the'Cenozoic'of the JVM heap
   MaxNewSize               = 1393033216 (1328.5MB)//Corresponding to the JVM startup parameter - XX:MaxNewSize = Setting the maximum size of the'Cenozoic'of the JVM heap
   OldSize                  = 175112192 (167.0MB)//Corresponding to the JVM startup parameter - XX: OldSize= < value >: Setting the size of the'Old Generation'of the JVM heap
   NewRatio                 = 2//Corresponding to the jvm startup parameter - XX:NewRatio=:'Cenozoic'and'Old Generation' size ratio
   SurvivorRatio            = 8//The corresponding jvm startup parameter - XX:SurvivorRatio = Sets the size ratio of Eden area to Survivor area in the younger generation 
   MetaspaceSize            = 21807104 (20.796875MB)//The corresponding jvm startup parameter -- XX:MetaspaceSize is allocated to the class metadata space
   CompressedClassSpaceSize = 1073741824 (1024.0MB)//The jvm startup parameter - XX: CompressedClassSpace Size defaults to 1G, and I can guess that the author of metaspace does not want to have its OOM problems.
   MaxMetaspaceSize         = 17592186044415 MB//Corresponding to the jvm startup parameter XX:MaxMetaspaceSize, the largest set of local memory class employees can be used for garbage collection. By default, there is no limit, that is to say, what is the maximum memory limit of your system? If it is not specified, the Metaspace will be dynamically resized according to the needs of the application running.
   G1HeapRegionSize         = 0 (0.0MB)//When using the G1 collector, set the size of the java heap to be partitioned. This size ranges from 1M to 32M

Heap Usage://Heap memory usage
PS Young Generation
Eden Space://Heap memory usage
   capacity = 1317011456 (1256.0MB)//Heap memory usage
   used     = 46816712 (44.64789581298828MB)//Heap memory usage
   free     = 1270194744 (1211.3521041870117MB)//Heap memory usage
   3.554768774919449% used//Heap memory usage
From Space://Heap memory usage
   capacity = 524288 (0.5MB)
   used     = 0 (0.0MB)
   free     = 524288 (0.5MB)
   0.0% used
To Space://Heap memory usage
   capacity = 524288 (0.5MB)
   used     = 0 (0.0MB)
   free     = 524288 (0.5MB)
   0.0% used
PS Old Generation//Current memory distribution in Old region
   capacity = 312999936 (298.5MB)
   used     = 62510688 (59.614837646484375MB)
   free     = 250489248 (238.88516235351562MB)
   19.971469898319725% used

32160 interned Strings occupying 3634120 bytes.
[publish@YZ-PTEST-CUS-WEB-01 ~]$ 

It's clear what's going on in each area of the Java heap.

4.2.5 jhat: Virtual Machine heap dump snapshot analysis tool

  • Function: To analyze heapdump generated by jmap. It has built-in a miniature HTTP server, which can view the analysis results in the browser.
  • There are two main reasons why jhat is seldom used in practice: one is that the analysis project consumes server resources; the function is relatively simple compared with Bisual VM and IBM Heap Analyzer;

Command format

jhat [dumpfile]

parameter

  • -stack false|true
    Close the tracking object allocation call stack. If the allocation location information is not available in the heap dump, this flag must be set to false. The default value is true. >
  • -refs false|true
    Close tracking of references to objects. The default value is true. By default, the returned pointer refers to objects that are directed to other specific objects, such as backlinks or incoming references, and counts / calculates all objects in the heap. >
  • -port port-number
    Set the port number of jhat HTTP server. Default value 7000. >
  • -exclude exclude-file
    Data member list file (a file that lists data members that should be excluded from the reachable objects query) that needs to be excluded when specifying object queries. For example, if the file column lists java.lang.String.value, references involving java.lang.String.value are excluded when calculating the list of reachable objects from a particular object Object o. >
  • -baseline exclude-file
    Specify a baseline heap dump. Objects with the same object ID in two heap dumps are marked as not being new. Other objects are marked as new. It is useful to compare two different heap dumps. >
  • -debug int
    Setting debug level. 0 means no debugging information is output. The larger the value, the more detailed debug information is output.
  • -version
    Exit when only version information is displayed after startup
  • -J< flag >
    Because the jhat command actually starts a JVM to execute, some startup parameters can be passed in through - J when starting the JVM. For example, - J-Xmx512m specifies that the maximum heap memory used by the Java virtual machine running jhat is 512 MB. If multiple JVM startup parameters are required, multiple - Jxxxxxx xx can be passed in.
$ jhat -J-Xmx512m dump.hprof
Reading from dump.hprof...
Dump file created Mon Jul 31 23:46:31 CST 2017
Snapshot read, resolving...
Resolving 1285013 objects...
Chasing references, expect 257 dots.................................................................................................................................................................................................................................................................
Eliminating duplicate references.................................................................................................................................................................................................................................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.

The intermediate-J-Xmx512m allocates 512M memory to start the HTTP server when the dump snapshot is large. After running, the browser can open Http://localhost:7000 for snapshot analysis.
The heap snapshot analysis is mainly in Heap Histogram at the end, which lists all surviving objects at dump time according to class.

4.2.6 jstack: Java Stack Tracking Tool

Jstack is used to generate thread snapshots of the current time of the Java virtual machine. Thread snapshots are a collection of method stacks that are being executed by each thread in the current Java virtual machine. The main purpose of generating thread snapshots is to locate the causes of long thread pauses, such as deadlocks between threads, deadlocks, long waiting caused by requesting external resources, etc. When a thread pauses, it can look through the call stack of each thread through jstack to see what the unresponsive thread is doing in the background or waiting for what resources. If a java program crashes to generate a core file, the jstack tool can be used to obtain information about the java stack and native stack of the core file, so that it can easily know how the Java program crashes and where the problem occurs. In addition, the jstack tool can also be attached to the running Java program, and see the information of the java stack and native stack of the running Java program at that time. jstack is very useful if the running Java program is hung.

Command format

jstack [option] LVMID

option parameter

  • - F: Force the output thread stack when the normal output request is not responded to
  • - l: Display additional information about locks in addition to stacks
  • - m: If you call a local method, you can display the C/C++ stack
$ jstack -l 55671|more
2017-08-01 00:20:58
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.65-b01 mixed mode):

"DubboClientHandler-10.32.32.31:7070-thread-6208" #13942 daemon prio=5 os_prio=0 tid=0x00007f4a40002800 nid=0xd5f1 waiting on condition [0x00007f4af0b5f000]
   java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000006ca25a240> (a java.util.concurrent.SynchronousQueue$TransferStack)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
    at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
    at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

"DubboClientHandler-10.32.32.54:60036-thread-1219" #13941 daemon prio=5 os_prio=0 tid=0x00007f4a640f4800 nid=0xd5ec waiting on condition [0x00007f4ae818f000]
   java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000006ca76a638> (a java.util.concurrent.SynchronousQueue$TransferStack)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
    at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
    at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
    at java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None
--More--

Analysis

Here's an article that explains it well.
Analysis of printed documents

4.2.7 HSDIS: JIT Generated Code Disassembly

4.3 JDK Visualization Tool

4.3.1 JConsole: Java Monitoring and Management Console

4.3.2 Visual VM: Multi-in-one Fault Handling Tool

Deep Understanding of Java Virtual Machine: Advanced Features and Best Practices of JVM Zhou Zhiming. High Definition Scanning Edition. pdf

Download Address: Link: http://pan.baidu.com/s/1miBQCBY Password: 9kbn

To be continued

...

Recommended reading

Deep Understanding of Java Virtual Machines (1)
Deep Understanding of Java Virtual Machines (2)
Deep Understanding of Java Virtual Machine (3)

  • Author: Peng Lei
  • Source: http://www.penglei.net
  • Copyright belongs to the author. Please indicate the source for reprinting.

Reference resources

Almost all references in this article - Pure Smile - jvm Tuning - Command Chapter

The orders were all tested by themselves.

JVM command parameters of java high branch office (advanced garbage collection options)
Deep Exploration of JVM (2) - Secret Metaspace
http://www.jianshu.com/p/cd8f6c103525
Reference - Pure Smile - jvm Tuning - Command Chapter

Posted by jwagner on Sat, 08 Jun 2019 16:28:06 -0700