Explains common COMMANDS for the JVM in preparation for the following tutorial on JVM log analysis.

preface

While there are a number of JVM tuning tools out there, almost all of them rely on JDK interfaces and the underlying commands. Studying the use of these commands also gives us a better understanding of JVM composition and characteristics.

The Sun JDK monitoring and troubleshooting commands include JPS, jstat, jmap, jhat, jstack, and jinfo.

jps

JVM Process Status Tool, which displays all HotSpot VIRTUAL machine processes in a specified system.

Command format:

jps [options] [hostid]
Copy the code

The option parameter:

  • -l: Displays the full name of the main class or the JAR path
  • -q: Output only LVMID
  • -m: Prints the arguments passed to main() when the JVM starts
  • -v: displays the specified JVM parameters when the JVM starts

Example:

82518 sun.tools.jps.Jps -l -m 69981 /Users/mengloulv/Library/Application Support/JetBrains/IntelliJIdea2020.1 / plugins/idea - spring - the tools/lib/server/language - server jar in 79213 org.apache.catalina.startup.Bootstrap start 79212 org.jetbrains.jps.cmdline.Launcher /Applications/IntelliJ IDEA. The app/Contents/lib/asm - all - 7.0.1. Jar: XXX...Copy the code

jstat

Jstat (JVM Statistics Monitoring) is a command used to monitor the status of a virtual machine while it is running. It shows the running data of a virtual machine, such as class loading, memory, garbage collection, JIT compilation, and so on.

See: docs.oracle.com/javase/7/do…

Jstat is introduced

Jstat command Command format: jstat [Options] vmid [interval] [count] Parameter description: -gcutil or -gc To check the GC status PID, running Java process ID interval, the unit is seconds or milliseconds count, printing times. By default, the printing times are numerous. Options: -gc: -gcutil: Indicates the heap status during GC, expressed in bytes of space. -class: indicates the class loader behavior. -compile: indicates the class loader behavior. -gccapacity: collects information about the heap capacity of different generations. -gccause: collects information about the event that causes GC. -gcnew: collects information about the generation that causes GC. -gcold: indicates the current generation heap capacity during GC. -gcoldCapacity: indicates the current generation heap capacity during GC. -gcpermCapacity: indicates the current generation heap capacity during GCCopy the code

Jstat example

Example 1: jstat -gc 15 5000 5

The GC status of the Java process 15 is displayed every 5 seconds. An exception is generated every 5 seconds for a total of 5 times.

 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT   
307200.0 307200.0 24333.5  0.0   2457600.0 945456.5 5120000.0  3462367.2  241360.0 209218.1 26120.0 20538.3   6642  531.164  20      6.874  538.038
307200.0 307200.0 24333.5  0.0   2457600.0 945513.4 5120000.0  3462367.2  241360.0 209218.1 26120.0 20538.3   6642  531.164  20      6.874  538.038
307200.0 307200.0 24333.5  0.0   2457600.0 968130.4 5120000.0  3462367.2  241360.0 209218.1 26120.0 20538.3   6642  531.164  20      6.874  538.038
307200.0 307200.0 24333.5  0.0   2457600.0 1394418.3 5120000.0  3462367.2  241360.0 209218.1 26120.0 20538.3   6642  531.164  20      6.874  538.038
307200.0 307200.0 24333.5  0.0   2457600.0 1867238.5 5120000.0  3462367.2  241360.0 209218.1 26120.0 20538.3   6642  531.164  20      6.874  538.038
Copy the code
S0C: size of the first survivable zone S1C: size of the second survivable zone S0U: size of the first survivable zone S1U: size of the second survivable zone EC: size of the Eden zone EU: size of the Eden zone OC: size of the old age OU: size of the old age MC: size of the method zone MU: Method area usage size CCSC: size of the compressed space CCSU: size of the compressed space YGC: number of garbage collection times in the young generation YGCT: number of garbage collection times in the old generation FGC: number of garbage collection times in the old age FGCT: number of garbage collection times in the old age GCT: total garbage collection time unit: KBCopy the code

I can calculate the following core data:

  • The size of the first surviving zone is S0C: 300M
  • The size of the second surviving zone is S1C: 300M
  • Size of Eden Park EC: 2400M
  • Old age size OC: 5000M
  • Method area size MC: 236M
  • Waste recovery time of young generation YGCT: 531.164 (unit?)
  • Old age garbage collection time FGCT: 6.874

Let’s look at the output GC log:

Heap before GC invocations=6641 (full 10): par new generation total 2764800K, used 2492979K [0x00000005cc000000, 0x0000000687800000, 0x0000000687800000) eden space 2457600K, 100% used [0x00000005cc000000, 0x0000000662000000, 0x0000000662000000) from space 307200K, 11% used [0x0000000674c00000, 0x0000000676e8cc90, 0x0000000687800000) to space 307200K, 0% used [0x0000000662000000, 0x0000000662000000, 0x0000000674c00000) concurrent mark-sweep generation total 5120000K, used 3462278K [0x0000000687800000, 0x00000007c0000000, 0x00000007c0000000) Metaspace used 209218K, capacity 229352K, committed 241360K, Reserved 1265664K Class space used 20538K, Capacity 24038K, committed 26120K, reserved 1048576K 343501.719: [Allocation Failure (GC) 343501.719: [ParNew: Secs] [Times: 3287k -> 3287k, 3287k] [Times: 3287k, 3287k] [Times: 3287k, 3287k] User sys = = 0.05 0.01, real = 0.03 secs]Copy the code

The following core data can be calculated:

  • The size of the first surviving zone is S0C: 300M
  • The size of the second surviving zone is S1C: 300M
  • Size of Eden Park EC: 2400M
  • Old age size OC: Cannot be calculated from here
  • Method area size MC: cannot be calculated from here
  • GC time: 30ms

The JAVA_OPTS parameters I set are as follows:

-Xmx8000M -Xms8000M -Xmn3000M -XX:PermSize=1000M -XX:MaxPermSize=1000M -Xss256K -XX:+DisableExplicitGC -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=69 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:/home/work/logs/applogs/gc.log - javaagent: / home/work/app/Prometheus/jmx_prometheus_javaagent - 0.12.0. Jar = 3010: / home/work/app/Prometheus/JMX - exporter. YmlCopy the code

Here I have two questions:

  • Question 1:2764800/1024=2700M, what is 2700M? Is it EC plus S0C, or EC plus S1C? “Current available capacity for the new generation”? (Because only one of S0C and S1C will be used each time)
  • Question 2: where 7884800K/1024=7700M, what is this 7700M? OC+EC+S0C, or EC+S1C? The old + “current available capacity of the new generation”?

Example 2: jstat -gccapacity 15

Same as -gc, but also prints the maximum and minimum space used by each region of the Java heap

NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC MCMN MCMX MC CCSMN CCSMX CCSC YGC FGC 819200.0 819200.0 819200.0 273024.0 273024.0 273152.0 5324800.0 5324800.0 5324800.0 0.0 1251328.0 223560.0 0.0 1048576.0 22716.0 174 8Copy the code
NGCMN Minimum new generation capacity (KB). NGCMX Maximum new generation capacity (KB). NGC Current new generation capacity (KB). S0C Current survivor space 0 capacity (KB). S1C Current survivor space 1 capacity (KB). EC Current eden space capacity (KB). OGCMN Minimum old generation capacity (KB). OGCMX Maximum old generation capacity (KB). OGC Current  old generation capacity (KB). OC Current old space capacity (KB). PGCMN Minimum permanent generation capacity (KB). PGCMX Maximum Permanent generation capacity (KB). PGC Current Permanent generation capacity (KB). PC Current Permanent space capacity (KB). YGC Number of Young generation GC Events. FGC Number of Full GC Events.Copy the code

Example 3: jstat -gcutil 15

Same as -gc, but prints the percentage of the total used space

See: docs.oracle.com/javase/7/do…

   S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT   
  0.32   0.00  27.68   9.86  98.17  96.03    174   24.437     8    0.720   25.157
Copy the code

Example 4: jstat-gccause 15

Overview of garbage collection statistics (same as -gcutil), plus the cause of the last two garbage collection events

See: docs.oracle.com/javase/7/do…

   S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC                 
  0.32   0.00  43.15   9.86  98.17  96.03    174   24.437     8    0.720   25.157 Allocation Failure   No GC
Copy the code
LGCC: cause of the latest garbage collection GCC: cause of the current garbage collectionCopy the code

Example 5: jstat -gcnew 15

Measure the behavior of the new generation

See: docs.oracle.com/javase/7/do…

S0C S1C S0U S1U TT MTT DSS EC EU YGC YGCT 273024.0 273024.0 862.5 0.0 15 136512.0 273152.0 119572.5 174 24.437Copy the code

Example 6: jstat -gcold 15

Measure the behavior of the new generation

See: docs.oracle.com/javase/7/do…

   MC       MU      CCSC     CCSU       OC          OU       YGC    FGC    FGCT     GCT   
223560.0 219467.9  22716.0  21813.1   5324800.0    524929.7    174     8    0.720   25.157
Copy the code

Example 7: jstat -class 15

Monitor class loads, unload numbers, total space, and elapsed time.

Loaded  Bytes  Unloaded  Bytes     Time   
 39163 74053.8    11505 17286.0      46.52
Copy the code
Loaded: the number of Unloaded classes Bytes: the number of Unloaded classes Bytes: the size of the Unloaded class Time: the loading TimeCopy the code

Example 8: jstat-Compiler 15

Output the number of JIT-compiled methods, time, etc.

Compiled Failed Invalid Time FailedType FailedMethod 53393 4 0 1 com/mysql 575.86 / JDBC/AbandonedConnectionCleanupThread runCopy the code
Compiled: Failed: Number of compilation failures Invalid: number of Invalid Time: compilation Time FailedType: type of a failure FailedMethod: name of a Failed methodCopy the code

jmap

Example 1: jmap-dump :live,format=b,file=dump.hprof 15

Dump the heap to a file,format specifies the output format, live specifies the live object, and file specifies the file name

Dumping heap to /Users/mengloulv/java-workspace/dump.hprof ...
Heap dump file created
Copy the code

The dump.hprof suffix is designed to be opened directly with THE Memory Anlysis Tool (MAT).

Example 2: jmap-finalizerInfo 15

Prints information about objects waiting to be reclaimed

Attaching to process ID 15, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.202-b08
Number of objects pending for finalization: 0
Copy the code

No object in the f-Queue is waiting for the Finalizer thread to execute the Finalizer method

Example 3: jmap-heap 15

Print heap summary information, GC algorithm used, heap configuration and WISE heap usage, can be used to determine the current use of memory and garbage collection, feel this very use!

Attaching to process ID 15, please wait... Debugger attached successfully. Server Compiler detected. JVM version is 25.202-B08 using Parallel Threads in the new // Concurrent mark-sweep GC Heap Configuration: MinHeapFreeRatio = 40 / Corresponding JVM startup parameter -xx :MinHeapFreeRatio Sets the minimum heap idle ratio of the JVM (default 40) MaxHeapFreeRatio = 70 -xx :MaxHeapFreeRatio Sets the maximum heap free ratio of the JVM (default 70) MaxHeapSize = 8388608000 (8000.0MB) // The JVM startup parameter -xx :MaxHeapSize= set the maximum size of the JVM heap NewSize= 3145728000 (3000.0MB) // The JVM startup parameter -xx :NewSize= set the default size of the JVM heap 'new generation' MaxNewSize= 3145728000 (3000.0MB)/corresponding JVM startup parameter -xx :MaxNewSize= Set the maximum size of the 'new generation' of the JVM heap OldSize = 5242880000 (5000.0MB) // The corresponding JVM startup parameter -xx :OldSize=<value>: sets the size of the 'old generation' of the JVM heap NewRatio= 2 // The corresponding JVM startup parameter -xx :NewRatio=: the size ratio of the 'new generation' and the 'old generation' SurvivorRatio = 8 // Corresponding JVM startup parameter -xx :SurvivorRatio= Set the size ratio of Eden zone to Survivor zone in the young generation MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB // Corresponding JVM startup parameter -xx :MaxPermSize=<value>: Sets the maximum eternal generation size of the JVM heap G1HeapRegionSize = 0 (0.0MB) Heap Usage: // Heap Usage New Generation (Eden + 1 Survivor Space): Capacity = 2831155200 (2700.0MB) Used = 1819140416 (1734.8674926757812MB) Free = 1012014784 (965.1325073242188MB) 64.2543515805845% informs the Eden Space: Capacity = 2516582400 (2400.0MB) Used = 1795637848 (1712.4536972045898MB) Free = 720944552 (687.5463027954102MB) 71.35223738352458%, informs the From Space: Capacity = 314572800 (300.0MB) Used = 23502568 (22.413795471191406MB) Free = 291070232 (277.5862045288086MB) 7.471265157063802%, informs the To Space: Capacity = 314572800 (300.0MB) Used = 0 (0.0MB) Free = 314572800 (300.0MB) 0.0% Used concurrent mark-sweep generation: Capacity = 5242880000 (5000.0MB) Used = 3448095536 (3288.360153198242MB) Free = 1794784464 (1711.6398468017578MB) 65.76720306396484% used 101426 Interned Strings eviction 10749600 bytes.Copy the code

Example 4: jmap – histo: 15 | live more

Dump :live is a full GC, so the size of the heap without LIVE is larger than the size of the heap with live). Only the first 15 rows are printed.

  num     #instances         #bytes  class name
----------------------------------------------
   1:        938552      113143448  [C
   2:        983711       31478752  java.util.HashMap$Node
   3:        930339       22328136  java.lang.String
   4:         61854       21628224  [B
   5:        215981       19006328  java.lang.reflect.Method
   6:        200183       18164992  [Ljava.lang.Object;
   7:        121341       16297048  [Ljava.util.HashMap$Node;
   8:        511306       12919376  [Ljava.lang.String;
   9:        169168        9391000  [I
  10:        165488        6619520  java.util.LinkedHashMap$Entry
  11:        131563        6315024  org.hibernate.hql.internal.ast.tree.Node
  12:        122202        5865696  java.util.HashMap
  13:        320105        5121680  java.lang.Integer
  14:        204087        4898088  java.util.ArrayList
  15:        138888        4444416  java.util.concurrent.ConcurrentHashMap$Node
  ... ...
Copy the code

XML class name is an object type, described as follows:

B byte C char D double F float I int J long Z Boolean [array, e.g. [I denotes int[] [L+ class name other objectCopy the code

jhat

The JHAT (JVM Heap Analysis Tool) command is used with Jmap to analyze dump results generated by Jmap. Jhat has a built-in miniature HTTP/HTML server. After dump Analysis results are generated, you can view them in a browser. Note that the dump file generated by the server is usually copied to local or other machines for analysis because jHAT is a time-consuming and resource-consuming process.

Example: jhat dump.hprof

When the execution is completed:

It can be accessed at Http://localhost:7000:

Check whether a large number of objects that should be reclaimed are referenced all the time or whether objects that occupy too much memory cannot be reclaimed. In most cases, the system will go down to the client and use the tool to analyze it.

jstack

Jstack is used to generate a thread snapshot of the Java VIRTUAL machine at the current time. A thread snapshot is a collection of method stacks that are being executed by each thread in the Java VIRTUAL machine (JVM). The main purpose of a thread snapshot is to locate the cause of a long pause in a thread, such as deadlocks, loops, and long waits caused by requests for external resources. By looking at the call stack of each thread when it pauses, you can see what the unresponsive thread is doing in the background, or what resources it is waiting for.

If a Java program crashes to generate a core file, the JStack tool can be used to get information about the Java Stack and native stack of the core file, so you can easily know how the Java program crashed and where the problem occurred in the program.

In addition, the JStack tool can attach to a running Java program and see information about the Java Stack and native Stack of the running Java program. Jstack is very useful if the current running Java program appears hung.

Command format:

jstack [option] LVMID
Copy the code

The option parameter:

-f: forces the thread stack to be output when a normal output request is not responded to. -l: displays additional information about the lock in addition to the stack. -m: displays the C/C++ stack if a local method is calledCopy the code

Example: jstack – 5073 l | more

Welcome everyone to like a lot, more articles, please pay attention to the wechat public number “Lou Zai advanced road”, point attention, do not get lost ~~