Detailed explanation of the use of commonly used JVM tuning tools JPS, JMAP, JSTAT, JSTACK and JCMD

View PID information

First start a service

 The difference between using jps and jps -l, using jps -l can display the name of the service

 Familiar with JMAP, JSTAT and JSTACK, commonly used tools in JVM tuning

JMAP, JSTAT, and JSTACK are commonly used tools in Java development for analyzing and debugging Java applications. Their usage scenarios are as follows:

  • JMAP: JMAP is used to generate Java heap dump snapshots in order to analyze the memory usage of Java applications. It can provide information about the number, type, size, etc. of objects to help identify memory leaks or memory overflow problems.
  • JSTAT: JSTAT is used to monitor and collect various statistics of the Java Virtual Machine (JVM), such as garbage collection, class loading, number of threads, etc. It can display these statistics in real time to help developers understand the performance status of their applications.
  • JSTACK: JSTACK is used to generate Java thread dump snapshots to analyze the thread status and call stack information of Java applications. It can help developers locate deadlocks, infinite loops, thread blocking and other problems, and provide thread running traces, which is helpful for thread-level troubleshooting.
  • JCMD is a multifunctional tool. It is a more comprehensive tool that can be used to obtain the performance statistics, memory usage, garbage collection, thread stack, JVM runtime, GC, exported thread information, heap information, etc. of the target Java process. , simply understood as it can directly use the relevant name to obtain information

To sum up, JMAP is suitable for memory analysis, JSTAT is suitable for performance monitoring, and JSTACK is suitable for thread analysis. Depending on specific problems and needs, choosing the right tool can better help developers debug and optimize.

Use of JMAP

JMAP is a command line tool for generating Java heap dump snapshots.

The following is the use of JMAP and analysis of common attributes:

Usage:
Enter jmap -<option> <pid>, where <option> is the JMAP option and <pid> is the process ID of the Java process.


Commonly used options (properties):

  • heap: Generate Java heap dump snapshot. Example: jmap -heap <pid>
  • histo: Generates statistical information about objects in the Java heap, including the number, type, and size of objects. Example: jmap -histo <pid>
  • dump: Generate a Java heap dump snapshot and save it to a file. Example: jmap -dump:format=b,file=<filename> <pid>
  • finalizerinfo: Displays object information waiting for finalization. Example: jmap -finalizerinfo <pid>
  • clstats: Display class loader statistics. Example: jmap -clstats <pid>
  • permstat: Displays statistics of the permanent generation (PermGen/Metaspace). Example: jmap -permstat <pid>

It can also be viewed in the service:

For example: jmap -heap <pid>  

Parameter description:  
Heap Configuration: #Heap configuration 
   MinHeapFreeRatio #Heap minimum usage ratio
   MaxHeapFreeRatio # Maximum heap usage ratio
   MaxHeapSize                                                                                                                                                                              #The maximum capacity that can be used by the new generation Size    OldSize                                                                                                                                                                                                    ​ #The proportion of new generation and survivor    G1HeapRegionSize #Block size    MaxMetaspaceSize #Maximum value of metadata space , exceeding this value will trigger GC overflow (JVM will dynamically change this value)    CompressedClassSpaceSize #Class pointer compression space size, the default is 1G    MetaspaceSize #Initial size of metadata space








Heap Usage:
G1 Heap:
   regions   #G1 block initialization size
   capacity   #G1 block Maximum usable size
   used G1 block usage ratio G1 Young Generation: #新代 Eden Space: #Eden area space    regions = 670< /span>    free = 1293942784 (1234.0MB)    9.221544492213267% used    free = 1443051744 (1376.2013854980469MB)    used = 146589472 (139.79861450195312MB)    capacity = 1589641216 (1516.0MB)    regions = 72 G1 Old Generation: #老generate    100.0% used    free = 0 (0.0MB)    used = 6291456 (6.0MB)    capacity = 6291456 (6.0 MB)    regions = 3 Survivor Space: #Survivor District    52.05905205905206% used    used = 1405091840 (1340.0MB)    capacity = 2699034624 (2574.0MB)




















Use of JSTAT

JSTAT is a command-line tool for monitoring and collecting Java Virtual Machine (JVM) statistics. It provides multiple options and properties for displaying different aspects of JVM statistics.

The following is the usage of JSTAT and the analysis of common attributes:

Usage:
Enter jstat -<option> <pid> [<interval> [<count>]]

Where <option> is the JSTAT option, <pid> is the process ID of the Java process,

<interval> is the sampling interval (in milliseconds), <count> is the number of samples.

Commonly used options (properties):

  • -class: Displays information such as the number of class loading, unloading and total space.
  • -compiler: Displays the compilation tasks and time-consuming information of the JIT compiler.
  • -gc: Displays garbage collector statistics, including heap memory usage, GC time, throughput, etc.
  • -gcutil: Display detailed statistics of the garbage collector, including heap memory usage, GC time, throughput, GC duration, etc.
  • -gccause displays the time and reason for GC triggering, etc.
  • -gccapacity: Displays detailed information on heap memory capacity and usage.
  • -gcnew: Display statistics of the new generation garbage collector.
  • -gcnewcapacity: Displays detailed information about the new generation heap memory capacity and usage.
  • -gcold: Display statistics of the old generation garbage collector.
  • -gcoldcapacity: Displays detailed information about the old generation heap memory capacity and usage.
  • -gcpermcapacity: Displays detailed information about permanent generation (PermGen/Metaspace) capacity and usage.

You can also view specific options in Linux:

For example: jstat -gcutil 23397 1000 15 -- print once per second, print 15 times

 It can also be written as follows:

By checking the number and time of the new generation, old generation and GC, and adjusting the service startup parameter settings

Parameter analysis:

  • S0: The usage percentage of Survivor 0.
  • S1: Usage percentage of Survivor 1.
  • E: Usage percentage of Eden area.
  • O: The usage percentage of the Old Generation.
  • M: Metaspace usage percentage.
  • CCS: The usage percentage of Compressed Class Space.
  • YGC: The number of young generation garbage collections.
  • YGCT: Total young generation garbage collection time in seconds.
  • FGC: The number of Full GC (global garbage collection).
  • FGCT: Total Full GC (global garbage collection) time in seconds.
  • GCT: Total time in seconds for all garbage collections.
  • NGCMN: The minimum capacity of the young generation (in KB).
  • NGCMX: The maximum capacity of the young generation (in KB).
  • NGC: The current capacity of the young generation (in KB).
  • OGCMN: The minimum capacity of the old generation (in KB).
  • OGCMX: The maximum capacity of the old generation (in KB).
  • OGC: Current capacity of the old generation (in KB).
  • MCMN: Minimum capacity of metaspace (in KB).
  • MCMX: Metaspace maximum capacity (in KB).
  • MC: Current capacity of metaspace (in KB).
  • CCSMN: Minimum capacity of compressed class space (in KB).
  • CCSMX: Maximum capacity of compressed class space (in KB).
  • CCSC: Current capacity of compression class space (in KB).

By analyzing these parameter information, you can understand the usage of the garbage collector, the usage and changing trends of heap memory, etc., to help identify memory problems and perform performance tuning.

For example: jstat -gccause 23397 1s 10 -- Check the reason for GC

Use of JSTACK

jstack is a command line tool for generating Java thread dump snapshots, which can help analyze and diagnose threading issues in Java applications.

Usage:
     Enter jstack [-m] <pid>, where <pid> is the process ID of the Java process.


Commonly used options (properties):

  • -l: Output a long-format thread dump, including lock information.
  • -F: Forces a thread dump to be generated when the target Java process becomes unresponsive.
  • -m: Output Java and local stack information.
  • -h: Display help information.

Output analysis:
The content output by jstack contains the stack trace information of all threads in the current Java process. The stack trace for each thread starts with the thread ID and shows the sequence of method calls according to the call hierarchy.

The following is an analysis of some common thread states and properties:

  • java.lang.Thread.State: Thread status, such as RUNNABLE (running), WAITING (waiting), TIMED_WAITING (timed waiting), etc.
  • at <class>.<method>(<file>:<line>): Stack trace information of method calls, including class name, method name and source code file location.
  • Locked <monitor>: The thread is holding a monitor lock.
  • Waiting on <monitor>: The thread is waiting for a monitor lock.
  • Blocked on <monitor>: The thread is blocked on a monitor lock.

By analyzing thread dump snapshots, you can understand the status of individual threads in a Java application, stack trace information, and possible deadlocks or performance issues. This is very helpful for diagnosing and solving thread-related issues.

Please note that the jstack command requires the same user rights as the target Java process to execute successfully. Also, it is recommended to use this command with caution in a production environment to avoid adverse effects on application performance.

For example: jstack -l 23397

Use of JCMD

Use the help documentation:

 Use jcmd to get the service process

Use jcmd <pid> help to get the executable command

For example: jcmd 23397 GC.heap_info

 For example: jcmd 23397 Thread.print

 In short, jcmd is a powerful tool that can help developers and operation and maintenance personnel diagnose, monitor and manage Java applications, thereby improving the reliability and performance of applications.

Guess you like

Origin blog.csdn.net/amosjob/article/details/131263751