Yige

Yige

Build

JVM Series - JVM Tuning

JVM Series - JVM Tuning#

Based on JDK8, content organized from:

  1. Jvm Series (4): Jvm Tuning - Command Edition
  2. Common Parameter Configuration for JVM Tuning
  3. Oracle JVM Official Link

GC Log Format#

Image referenced from Jvm Series (8): Overview of Jvm Knowledge Points

Young GC Log:
image.png

Full GC Log:
image.png

JVM Tuning Commands#

jps#

Command Format:
jps [options] [hostid]
Option Parameters

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

[option] and [hostid] parameters can also be omitted

Example

$ jps -l -m
  28920 org.apache.catalina.startup.Bootstrap start
  11589 org.apache.catalina.startup.Bootstrap start
  25816 sun.tools.jps.Jps -l -m

jstat#

Command Format
jstat [option] LVMID [interval] [count]
Parameters

  • [option] : Operation parameters
  • LVMID : Local virtual machine process ID
  • [interval] : Time interval for continuous output
  • [count] : Number of continuous outputs

Option Parameter Details#

image.png

-class#

Monitors the number of class loads, unloads, total space, and time consumed

$ jstat -class 11589
 Loaded  Bytes  Unloaded  Bytes     Time
  7035  14506.3     0     0.0       3.67
  • Loaded : Number of loaded classes
  • Bytes : Size of class in bytes
  • Unloaded : Number of unloaded classes
  • Bytes : Size of unloaded classes in bytes
  • Time : Loading time
-compiler#

Outputs the number of methods compiled by JIT and the time taken

$ jstat -compiler 1262
Compiled Failed Invalid   Time   FailedType FailedMethod
    2573      1       0    47.60          1 org/apache/catalina/loader/WebappClassLoader findResourceInternal
  • Compiled : Number of compilations
  • Failed : Number of compilation failures
  • Invalid : Number of invalid compilations
  • Time : Compilation time
  • FailedType : Type of failure
  • FailedMethod : Fully qualified name of the failed method
-gc#

Statistics on garbage collection heap behavior, commonly used command

$ jstat -gc 1262
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
22016.0 21504.0 15436.2  0.0   305664.0 90988.0   699392.0   15828.7   31024.0 30336.3 3632.0 3507.6     12    0.438   1      0.045    0.483
  • C means Capacity, U means Used

  • S0C : Total capacity of survivor0 area

  • S1C : Total capacity of survivor1 area

  • S0U : Used capacity of survivor0 area

  • S1U : Used capacity of survivor1 area

  • EC : Total capacity of Eden area

  • EU : Used capacity of Eden area

  • OC : Total capacity of Old area

  • OU : Used capacity of Old area

  • MC : Metaspace capacity

  • MU : Used capacity of Metaspace

  • CCSC : Compressed class space capacity

  • CCSU : Used capacity of Compressed class space

  • YGC : Number of young generation garbage collections

  • YGCT : Time for young generation garbage collection

  • FGC : Number of old generation garbage collections

  • FGCT : Time for old generation garbage collection

  • GCT : Total time consumed by garbage collection

-gccapacity#

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

$ jstat -gccapacity 1262
  NGCMN    NGCMX     NGC     S0C   S1C       EC      OGCMN      OGCMX       OGC         OC       MCMN     MCMX      MC     CCSMN    CCSMX     CCSC    YGC    FGC
349184.0 349184.0 349184.0 22016.0 21504.0 305664.0   699392.0   699392.0   699392.0   699392.0      0.0 1077248.0  31024.0      0.0 1048576.0   3632.0     12     1
  • NGCMN : Minimum space occupied by the young generation
  • NGCMX : Maximum space occupied by the young generation
  • NGC : Current young generation capacity
  • EC : Current young generation Eden area capacity
  • OGCMN : Minimum space occupied by the old generation
  • OGCMX : Maximum space occupied by the old generation
  • OGC:Current old generation capacity (KB)
  • OC:Current old generation space (KB)
  • MCMN : Minimum space occupied by metaspace
  • MCMX : Maximum space occupied by metaspace
  • MC : Current metaspace capacity
  • CCSMN : Minimum space occupied by Compressed class space
  • CCSMX : Maximum space occupied by Compressed class space
  • CCSC : Capacity of Compressed class space
-gcutil#

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

$ jstat -gcutil 28920
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT
 12.45   0.00  33.85   0.00   4.44  4       0.242     0    0.000    0.242
-gccause#

Overview of garbage collection statistics (same as -gcutil), with the reasons for the last two garbage collection events

$ jstat -gccause 28920
  S0     S1     E      O      P       YGC     YGCT    FGC    FGCT     GCT    LGCC                 GCC
 12.45   0.00  33.85   0.00   4.44      4    0.242     0    0.000    0.242   Allocation Failure   No GC
  • LGCC:Reason for the last garbage collection
  • GCC:Reason for the current garbage collection
-gcnew#

Statistics on the behavior of the young generation

$ jstat -gcnew 28920
 S0C      S1C      S0U        S1U  TT  MTT  DSS      EC        EU         YGC     YGCT
 419392.0 419392.0 52231.8    0.0  6   6    209696.0 3355520.0 1172246.0  4       0.242
  • TT:Tenuring threshold (the age required to promote to the old generation)
  • MTT:Maximum tenuring threshold
  • DSS:Size of survivor area (KB)
-gcnewcapacity#

Statistics on the young generation and its corresponding memory space

$ jstat -gcnewcapacity 28920
  NGCMN      NGCMX       NGC      S0CMX     S0C     S1CMX     S1C       ECMX        EC        YGC   FGC
 4194304.0  4194304.0  4194304.0 419392.0 419392.0 419392.0 419392.0  3355520.0  3355520.0     4     0
  • 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#

Statistics on the behavior of the old generation

$ jstat -gcold 28920
   PC       PU        OC           OU       YGC    FGC    FGCT     GCT
1048576.0  46561.7   6291456.0     0.0      4      0      0.000    0.242
-gcoldcapacity#

Statistics on the size and space of the old generation

$ jstat -gcoldcapacity 28920
   OGCMN       OGCMX        OGC         OC         YGC   FGC    FGCT     GCT
  6291456.0   6291456.0   6291456.0   6291456.0     4     0    0.000    0.242
-gcmetacapacity#

Statistics on metaspace behavior

$ jstat -gcmetacapacity 3491
   MCMN       MCMX        MC       CCSMN      CCSMX       CCSC     YGC   FGC    FGCT     GCT
       0.0  1077248.0    31024.0        0.0  1048576.0     3632.0    12     1    0.045    0.483
-printcompilation#

Statistics on hotspot compiled methods

$ jstat -printcompilation 28920
    Compiled  Size  Type Method
    1291      78     1    java/util/ArrayList indexOf
  • Compiled:Number of executed compilation tasks
  • Size:Size of method bytecode in bytes
  • Type:Compilation type
  • Method:Class name and method name of the compiled method. Class names use “/” instead of “.” as a space separator. Method names are given as the method name of the class. The format is consistent with the HotSpot - XX:+PrintCompilation option

jmap#

The jmap (JVM Memory Map) command is used to generate heap dump files. If this command is not used, the -XX:+HeapDumpOnOutOfMemoryError parameter can be used to automatically generate a dump file when the virtual machine encounters an OOM. jmap can not only generate dump files but also query detailed information about the finalize execution queue, Java heap, and permanent generation, such as current usage, which collector is currently being used, etc.

Command Format
jmap [option] LVMID

Option Parameters

  • -dump : Generate heap dump snapshot
  • -finalizerinfo: Display objects waiting for the Finalizer thread to execute the finalizer method in the F-Queue queue
  • -heap : Display detailed information about the Java heap
  • -histo : Display statistics of objects in the heap
  • -permstat : To print permanent generation statistics
  • -F : Force the generation of a dump snapshot when -dump is unresponsive

jhat#

The jhat (JVM Heap Analysis Tool) command is used in conjunction with jmap to analyze the dump generated by jmap. jhat has a built-in mini HTTP/HTML server, and after generating the analysis results of the dump, they can be viewed in a browser. It should be noted that analysis is generally not performed directly on the server, as jhat is a time-consuming and resource-intensive process; it is generally recommended to copy the dump file generated on the server to a local or other machine for analysis.

Command Format
jhat [dumpfile]
Parameters

  • -stack false|true Disable tracking of object allocation call stack. If 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 Disable tracking of references to objects. The default value is true. By default, returned pointers are objects pointing to other specific objects, such as backlinks or incoming references, and will count/calculate all objects in the heap.
  • -port port-number Set the port number for the jhat HTTP server. The default value is 7000.
  • -exclude exclude-file Specify a file that lists data members that should be excluded from the reachable objects query. For example, if the file lists java.lang.String.value, then when calculating the reachable object list from a specific object Object o, any reference path involving java.lang.String.value will be excluded.
  • -baseline exclude-file Specify a baseline heap dump. Objects with the same object ID in two heap dumps will be marked as not new. Other objects will be marked as new. This is useful when comparing two different heap dumps.
  • -debug int Set the debug level. 0 means no debug information is output. The larger the value, the more detailed the debug information is output.
  • -version Exit after displaying only version information upon startup.
  • -J< flag > Since the jhat command actually starts a JVM to execute, parameters can be passed to the JVM at startup through -J. 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 needed, multiple -Jxxxxxx can be passed.

jstack#

jstack is used to generate a snapshot of the threads of the Java virtual machine at the current moment. A thread snapshot is a collection of the method stacks that each thread in the current Java virtual machine is executing. The main purpose of generating a thread snapshot is to locate the reasons for long pauses in threads, such as deadlocks between threads, infinite loops, or long waits caused by requests for external resources.

Command Format
jstack [option] LVMID

Option Parameters

  • -F : Force output of the thread stack when normal output requests are unresponsive
  • -l : Display additional information about locks in addition to the stack
  • -m : If a native method is called, it can display the C/C++ stack

jinfo#

jinfo (JVM Configuration info) command is used to view and adjust the virtual machine runtime parameters in real-time. The previous jps -v command could only display specified parameters; if you want to view the values of parameters that are not displayed, you need to use the jinfo command.

Command Format
jinfo [option] [args] LVMID

Option Parameters

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

JVM Tuning Configuration Parameters#

Heap Configuration#

  • -Xms: Set the minimum size of the heap
  • -Xmx: Set the maximum size of the heap
  • -XX:NewSize=n: Set the size of the young generation
  • -XX:NewRatio=n: Set the ratio of the young generation to the old generation. For example, 3 means the ratio of young generation to old generation is 1:3, with the young generation occupying 1/4 of the total space of the young and old generations.
  • -XX:SurvivorRatio=n: The ratio of the Eden area to the two Survivor areas in the young generation. Note that there are two Survivor areas. For example, 3 means Eden: 3 Survivor: 2, with one Survivor area occupying 1/5 of the entire young generation.
  • -XX:MaxPermSize=n: Set the size of the permanent generation.

Collector Settings#

  • -XX:+UseSerialGC: Set the serial collector
  • -XX:+UseParallelGC: Set the parallel collector
  • -XX:+UseParallelOldGC: Set the parallel old generation collector
  • -XX:+UseConcMarkSweepGC: Set the concurrent collector

Parallel Collector Settings#

  • -XX:ParallelGCThreads=n: Set the number of CPUs used during collection by the parallel collector. Number of parallel collection threads.
  • -XX:MaxGCPauseMillis=n: Set the maximum pause time for parallel collection (if this time is reached and garbage collection is still not completed, it will stop collection).
  • -XX:GCTimeRatio=n: Set the percentage of garbage collection time to program runtime. The formula is: 1/(1+n).
  • -XX:+CMSIncrementalMode: Set to incremental mode. Suitable for single CPU situations.
  • -XX:ParallelGCThreads=n: Set the number of CPUs used when the concurrent collector collects the young generation in parallel. Number of parallel collection threads.

Garbage Collection Statistics Configuration#

  • -XX:+PrintGC
  • -XX:+PrintGCDetails
  • -XX:+PrintGCTimeStamps
  • -Xloggc:filename

JVM Tuning Case Studies#

Reference Links:

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.