1. Introduction

In general, there is a lot of practice and optimization inside the JVM, so it is common to use Java tools to troubleshoot performance problems and find code that affects performance, and then consider tuning JVM parameters as a last choice.

2. Standard parameters

The standard argument starts with – and is implemented by all JVMS

-help Displays the help information about the Java command

– server-client Specifies the vm running mode. The client mode is faster to start but less efficient in running performance and memory management than the Server mode. On the contrary, the server mode is slower to start than the client mode, but the runtime performance and memory management efficiency are higher. The default setting of the 64-bit version is server

-version Displays the Java version. -cp -classpath Specifies the CLASspath path

-jar Specifies a JAR package to run. The manifest file of the JAR package must specify main-class

-verbose Displays information about classes loaded by the JVM

-verbose:gc Displays GC information

-verbose: JNI prints native method call information

And so on,…

3. Non-standard parameters

Resources: sourl.cn/sx6zLt

Nonstandard parameters start with -x and give the JVM the ability to implement these parameters, but not all JVMS have these capabilities. They are also subject to change between JDK versions, so these parameters are not standard

-xint explain execution -xcomp compile execution, compile native code on first use -xmixed mode, [www@localhost ~]$java-xint -version Java version “11.0.9” 2020-10-20 LTS Java(TM) SE Runtime Environment 18.9 (Build 11.0.9+7-LTS) Java HotSpot 64-bit Server VM 18.9 (Build 11.0.9+7-LTS, Interpreted mode)

[www@localhost ~]$Java -xcomp -version Java version “11.0.9” 2020-10-20 LTS Java(TM) SE Runtime Environment 18.9 (build Java HotSpot(TM) 64-bit Server VM 18.9 (Build 11.0.9+7-LTS, Compiled mode)

[www@localhost ~] java-xmixed-version Java version “11.0.9” 2020-10-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (Build 11.0.9+7-LTS, mixed mode) \[www@localhost ~\]

-xms eg: -xms1g Specifies that the initial MEMORY of the JVM is 1 GB

-xmx eg: -XMx1g Specifies that the maximum available memory of the JVM is 1 GB

-xmn eg: -xmn1g Specifies that the capacity of the young generation is 1 GB. Increasing the capacity of the young generation reduces the capacity of the old generation

– xss-xss1m: sets the thread stack size. The default size is 1m

-xloggc eg: -xloggc :gc.log records GC logs to the gc.log file

-Xprof tracks running programs and outputs trace data in standard output

And so on,…

Unstable parameter no Stable, unstable parameter starts with -xx, the most used parameter type, non-standard parameter, relatively unstable, mainly used for JVM tuning and Debug

Boolean type format: -xx [+/-], + indicates enabled, – indicates disabled Example: -xx :+UseConcMarkSweepGC, indicates enabled CMS garbage collector Non-Boolean type format: -xx = Indicates that the name attribute is value Example: -xx :MaxGCPauseMillis=500 -xx :PermSize Non-heap, also can be said that the method area, -xx :MaxPermSize method area, Maximum memory size -xx: -useserialgc using serial GC -xx: -useParallelGC using parallel GC -xx: -useconcMarkSweepGC using Concurrent Mark And Sweep (CMS)GC for older ages Parameter Meaning Description -xx :CICompilerCount Maximum number of concurrent compilations If the value is greater than 1, the compilation speed is improved, but system stability is affected. -xx :InitialHeapSize=100M short for InitialHeapSize -xms100m -xx :MaxHeapSize=100M short for maximum heap size -xms100m -xx :NewSize=20M sets the size of the young generation -xx :OldSize=50M Set the old age size -xx :MetaspaceSize=50M Set the method area size -xx :MaxMetaspaceSize=50M Maximum method area size -xx :+UseParallelGC -xx :+UseParallelOldGC :+UseParallelOldGC :+UseParallelOldGC: Throughput priority -xx :+UseConcMarkSweepGC with CMS on old generation, pause time priority -xx :+UseG1GC with G1GC on new generation, old generation, Pause time priority -xx :NewRatio The Ratio of new and old generations. For example, -xx :Ratio=4 indicates the Cenozoic era: -xx :SurvivorRatio=8 (S0+S1):Eden=2:8, Is 1/10 of the whole new generation of a S – XX: + HeapDumpOnOutOfMemoryError start-up heap memory to print When the JVM heap memory overflow occurs, which is OOM, Automatic generation of dump file -xx :HeapDumpPath=heap.hprof Specifies the heap overflow print directory to generate a heap. Hprof file -xx :+PrintGCDetails -xx :+PrintGCTimeStamps in the current directory -xx :+PrintGCDateStamps -xloggc :gc.log Prints GC logs. Save to gc.log -xss128K Set stack size per thread the empirical value is 3000-5000 optimal -xx :MaxTenuringThreshold=15 The default value is 15 – XX: InitiatingHeapOccupancyPercent concurrent GC cycle starts heap memory usage ratio. The default value is 45 – XX: G1HeapWastePercent allows the waste heap space than the default is 10%, Mixedgg-xx :MaxGCPauseMillis=200M G1 Maximum pause time The pause time cannot be too small, or G1 will not be able to keep up with garbage generation. This results in the full GC. – XX: ConcGCThreads = n concurrent garbage collector using the number of threads a default value varies with the JVM running platform – XX: G1MixedGCLiveThresholdPercent = 65 mixed garbage collection cycles to be included in the old locale occupancy rate threshold Occupancy rate for 65% – XX: G1MixedGCCountTarget = 8 tag set cycle is completed, the upper limit of survival data for the old area of G1MixedGCLIveThresholdPercent execution hybrid recycling goal number of times the default 8 times the garbage collection, Mixed’s goal is to control within the target number – XX: describe Mixed GC G1OldCSetRegionThresholdPercent = 1. The Old Region is added to the CSet. The default occupation rate is 65%. -xx :LargePageSizeInBytes=4m Large page size for the Java heap. -xx :MaxHeapFreeRatio=70 Maximum proportion of free Java heap after GC -xx :MaxNewSize=size maximum memory usage of newly generated objects -xx :MaxPermSize=64m maximum memory usage of old generation objects -xx :MinHeapFreeRatio=40 minimum proportion of free space in the Java heap after GC -xx :ReservedCodeCacheSize=32m memory reserved for code -xx :ThreadStackSize=512 set the ThreadStackSize, If the value is 0, use the default value -xx :+UseLargePages Uses large page memory. -xx: -citime Prints the time consumed in JIT compilation. -xx :ErrorFile=./hs_err_pid.log Saves error logs or data to a file -xx: -ExtendeddTraceprobes Enables Solaris-specific DTrace probes -xx :OnError= “; “Run a custom command -xx :OnOutOfMemoryError=” after a fatal ERROR occurs; -xx: -printClAsshistogram prints the column information of the class instance when it encounters OOM for the first time. Same function as jmap-histo -xx: -printConCurrentLOCKS prints information about concurrent locks when ctrl-break occurs, Same function as jstack -l -xx: -printCommandlineFlags Prints flags that have appeared on the command line -xx: -printcompilation prints information about a method when it is compiled -xx: -traceclassLoading Track class loading information – XX: – TraceClassLoadingPreorder track by reference to all class loading information – XX: – TraceClassResolution track constant pool – XX: – TraceClassUnloading -xx: -traceloaderconstraints Trace information about classloader constraints [www@localhost ~]$Java -xx :+PrintFlagsFinal -version > log.txt Java Version “1.8.0_31” Java(TM) SE Runtime Environment (Build 1.8.0_31-B13) Java HotSpot(TM) 64-bit Server VM (build 25.31 – b07, mixed mode)

4. Common commands JPS The JPS command is used to view Java processes

The jps command lists the instrumented Java HotSpot VMs on the target system. The

command is limited to reporting information on JVMs for which it has the access

permissions.

[www@localhost ~] jps 4352 8480 Jps 1736 RemoteMavenServer 3144 SmartGit 7612 Bootstrap \[www@localhost ~\] jps -l

3024 sun.tools.jps.Jps

4352

1736 org.jetbrains.idea.maven.server.RemoteMavenServer

3144 SmartGit

7612 org.apache.catalina.startup.Bootstrap

The jinfo jinfo command is used to view and adjust the JVM configuration parameter syntax in real time:

Jinfo-flag name PID // Displays the value of the name attribute of a Java process. Jinfo-flag [+/-] PID // Enable Jinfo-flag = PID // Disables the JVM attribute Set the JVM parameter (name) to view the MaxHeapSize property of process 4352

[www@localhost ~]$jinfo-flag MaxHeapSize 4352-xx :MaxHeapSize=786432000 1 2 jinfo Changes the value of MaxHeapSize

Jinfo-flag MaxHeapSize=786432000 4352 1 jinfo-flag PID Displays the parameters that have been assigned values

[www@localhost ~]$ jinfo -flag 7612

Usage:

jinfo [option]

(to connect to running process)

jinfo [option] <executable

(to connect to a core file)

jinfo [option] [server_id@]

(to connect to remote debug server)

where is one of: -flag to print the value of the named VM flag -flag [+|-] to enable or disable the named VM flag -flag = to set the named VM flag to the given value -flags to print VM flags -sysprops to print Java system properties to print both of the above -h | -help to print this help message

Jstat Displays VM performance statistics The jstat Command performance statistics for an instrumented Java HotSpot VM. The target JVM is identified by its virtual machine identifier, or vmid option.

View the class loading information. For example, view the class loading information of a Java process every 1000 milliseconds for 10 times

[www@localhost ~]$jstat -class 3760 1000 10 Loaded Bytes Unloaded Bytes Time 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27 13080 26809.1 250 390.1 29.27

1 Check the garbage collection information

[www@localhost ~]$jstat -gc 3760 1000 10 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 492086.2 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 502965.7 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 502965.7 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775 24576.0 48640.0 767.1 0.0 528896.0 502965.7 214528.0 109639.1 89856.0 87 215.8 9472.0 8955.5 28 0.965 4 0.810 1.775

1

The jstack Command prints Java stack traces of Java Threads for a specified Java process, core file, or remote debug server.

Syntax: jStack PID

Two locks hold each other, causing a deadlock case:

public class LockDemo { public static void main(String[] args) { LockThread d1=new LockThread(true); LockThread d2=new LockThread(false); Thread t1=new Thread(d1); Thread t2=new Thread(d2); t1.start(); t2.start(); Public static Object obj1=new Object(); public static Object obj2=new Object(); } // thread1 holds the u1 lock, thread2 holds the U2 lock, thread1 waits to acquire the U2 lock, Thread2 Class LockThread implements Runnable{private Boolean flag; LockThread(boolean flag){ this.flag=flag; } public void run() { if(flag) { while(true) { synchronized(MyLock.obj1) { System.out.println(thread.currentThread ().getName()+” Get obj1 lock “); Synchronized (mylock.obj2) {system.out.println (thread.currentThread ().getName()+” obtain obj2 lock “); synchronized(mylock.obj2) {system.out.println (thread.currentThread ().getName()+” obtain obj2 lock “); }}}} else {while(true){synchronized(mylock. obj2) {system.out.println (thread.currentThread ().getName()+” obtain obj2 lock “); Synchronized (mylock.obj1) {system.out.println (thread.currentThread ().getName()+” Obtain obj1 lock “); synchronized(mylock.obj1) {system.out.println (thread.currentThread ().getName()+” obtain obj1 lock “); } } } } }

} 12 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2

[www@localhost ~]$ jps -l

2560

2624 SmartGit

3760 org.apache.catalina.startup.Bootstrap

5316 org.jetbrains.idea.maven.server.RemoteMavenServer

7400 sun.tools.jps.Jps

8104 LockDemo

1116 org.jetbrains.jps.cmdline.Launcher

[www@localhost ~]$ jstack 8104

1

2

3

4

5

6

7

8

9

10

11

The Jmap Command Prints shared object memory maps or heap memory details of a specified process, core file, or remote debug server.

Grammar:

jmap -heap PID

1

Dump heap memory information

jmap -dump:format=b,file=heap.hprof PID

1

Dump dump dump dump dump dump dump dump dump dump dump

+ HeapDumpOnOutOfMemoryError – – XX: XX: HeapDumpPath = heap. Write a Threadlocal hprof 1 example:

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@ RestController @ RequestMapping (value = {} “/ API”) public class ThreadLocalHeapController {/ / ThreadLocal examples of memory @GetMapping(value = “/testHeap”) public ResponseEntity
testHeap() { ThreadLocal

threadLocal = new ThreadLocal

(); try { threadLocal.set(new Byte[1024 * 1024]); return ResponseEntity.ok(“success”); }catch (Exception e) { return ResponseEntity.badRequest().build(); } finally {// threadlocal.remove (); // threadlocal.remove (); }}
[]>
[]>

}

1

2

3

25 Enable automatic dump of log files when the heap memory overflows

java -jar -Xms1000M -Xmx1000M -XX:+HeapDumpOnOutOfMemoryError – XX:HeapDumpPath=jvm.hprof Jvm-exception-example-0.0.1 – snapshot. jar 1 2 Perform AB pressure test on Linux: 10000 requests and 100 concurrent requests

[WWW @ localhost ~] ab – n10000 – c100http: / / 127.0.0.1:8080 / API/testHeapThisisApacheBench Version2.3 < ab – n – 10000 – c. 100 http://127.0.0.1:8080/api/testHeap This is ApacheBench, Version 2.3 < ab – n10000 – c100http: / / 127.0.0.1:8080 / API/testHeapThisisApacheBench Version2.3 < Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, www.zeustech.net/ Licensed to The Apache Software Foundation, www.apache.org/

Benchmarking 127.0.0.1 (be patient)

Completed 1000 requests

Completed 2000 requests

Completed 3000 requests

Completed 4000 requests

Completed 5000 requests

Completed 6000 requests

Completed 7000 requests

Completed 8000 requests

Completed 9000 requests

Completed 10000 requests

Finished 10000 requests

Server Software:

Server Hostname: 127.0.0.1

Server Port: 8080

Document Path: /api/testHeap

Document Length: 7 bytes

Concurrency Level: 100 Time Taken for Tests: 10.504 seconds Complete Requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 1390000 bytes HTML transferred: 70000 bytes Requests per second: 952.01 [#/ SEC] (mean Time per request: 105.041 [ms] (mean Time per request: 105.041) 1.050 [MS] (mean, across all concurrent requests) Transfer rate: 129.23 [Kbytes/ SEC] Received

Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 1 0.7 0 10

Processing: 7 104 43.1 100 1223

Waiting: 7 100 38.8 99 1213

Total: 7 104 43.1 101 1224

WARNING: The median and mean for the initial connection time are not within a normal deviation

These results are probably not that reliable.

Percentage of the requests served within a certain time (ms) 50% 101 66% 105 75% 108 80% 110 90% 119 95% 144 98% 185 99% 219 100% 1224 (longest request)

Jmap-heap PID prints the heap information again, you can see the concurrency situation, memory overflow

Of course, after getting the heap exception log, you can use GCViewer and so on according to the performance analysis. This blog is just to analyze a simple example, the production environment is particularly complex, requiring years of experience and patience