This kind of article is very difficult to write , One is JVM Huge number of parameters , Second, the content is boring , But I want to understand JVM Tuning is also an unavoidable link , This article is mainly used to summarize and sort out, so as to facilitate future reading , It mainly focuses on four aspects , Namely JVM Tuning parameters 、JVM Tuning method ( technological process )、JVM Tuning Tools 、JVM Tuning case , Tuning cases are currently being analyzed , It will be made up in the future .

## Garbage collection related parameters

Parameters of the part , Here's just a summary , For more details, please refer to Oracle Official website :JVM Command line parameter reference for

### Processor combination parameters

About JVM The difference between garbage processors , Reference resources :JVM Garbage location of tuning 、 Garbage collection algorithm 、 Garbage processor comparison

-XX:+UseSerialGC = Serial New (DefNew) + Serial Old

For small programs . This is not the option by default ,HotSpot According to the calculation and configuration JDK Version auto select collector

-XX:+UseParNewGC = ParNew + SerialOld

This combination is rarely used （ It's obsolete in some versions ）, Details refer to :Why Remove support for ParNew+SerialOld and DefNew+CMS in the future？

-XX:+UseConc(urrent)MarkSweepGC = ParNew + CMS + Serial Old

-XX:+UseParallelGC = Parallel Scavenge + Parallel Old (1.8 Default ) 【PS + SerialOld】

-XX:+UseParallelOldGC = Parallel Scavenge + Parallel Old

-XX:+UseG1GC = G1

Linux No default found in GC How to view , and windows Will print UseParallelGC

• java +XX:+PrintCommandLineFlags -version

Linux Next 1.8 What is the default garbage collector of the version ？

• 1.8.0_181 Default （ Cannot see ）Copy MarkCompact

• 1.8.0_222 Default PS + PO

### Virtual machine parameters

Parameter name meaning The default value is interpretative statement
-Xms Initial heap size Physical memory 1/64(<1GB) Default (MinHeapFreeRatio Parameters can be adjusted ) The free heap memory is less than 40% when ,JVM It's going to pile up until -Xmx The maximum limit of .
-Xmx Maximum heap size Physical memory 1/4(<1GB) Default (MaxHeapFreeRatio Parameters can be adjusted ) The free heap memory is larger than 70% when ,JVM It will reduce the pile until -Xms The minimum limit of
-Xmn Young generation size (1.4or lator) Be careful ： The size here is （eden+ 2 survivor space). And jmap -heap Shown in New gen Is different . The whole pile size = Young generation size + Old generation size + Lasting generation size . After increasing the younger generation , It will reduce the size of the old generation . This value has a great influence on the system performance ,Sun The official recommendation is to configure the entire heap as 3/8
-XX:NewSize Set younger generation size (for 1.3/1.4)
-XX:MaxNewSize Young generation maximum (for 1.3/1.4)
-XX:PermSize Set persistent generation (perm gen) Initial value Physical memory 1/64
-XX:MaxPermSize Set persistent generation maximum Physical memory 1/4
-Xss Stack size per thread JDK5.0 After that, the stack size of each thread is 1M, Previously, the stack size of each thread was 256K. More memory size required by application thread adjustment . In the same physical memory , Reducing this value can generate more threads . But the operating system has a limit on the number of threads in a process , Can't generate... Indefinitely , Experience is in 3000~5000 about Small applications in general , If the stack is not deep , Should be 128k Sufficient It is recommended to use 256k. This option has a big impact on performance , It needs strict testing . and threadstacksize The explanation of the options is very similar , The official documents do not seem to explain , There is such a sentence in the Forum :"” -Xss is translated in a VM flag named ThreadStackSize” Generally, you can set this value .
-XX:ThreadStackSize Thread Stack Size (0 means use default stack size) [Sparc: 512; Solaris x86: 320 (was 256 prior in 5.0 and earlier); Sparc 64 bit: 1024; Linux amd64: 1024 (was 0 in 5.0 and earlier); all others 0.]
-XX:NewRatio The younger generation ( Include Eden And two Survivor District ) The ratio to the old ( Remove the permanent generation ) -XX:NewRatio=4 The ratio of young generation to old generation is 1:4, The younger generation takes up the whole stack 1/5 Xms=Xmx And set up Xmn Under the circumstances , This parameter does not need to be set .
-XX:SurvivorRatio Eden District and Survivor Area size ratio Set to 8, Two Survivor District and a Eden The ratio of regions is 2:8, One Survivor District accounts for the whole young generation 1/10
-XX:LargePageSizeInBytes The size of the memory page cannot be set too large , It will affect Perm Size =128m
-XX:+UseFastAccessorMethods Fast optimization of the original type
-XX:+DisableExplicitGC close System.gc() This parameter needs to be strictly tested
-XX:MaxTenuringThreshold The maximum age of garbage If set to 0 Words , Then the young generation does not go through Survivor District , Go straight to the old generation . For the older generation more applications , Can improve efficiency . If you set this value to a larger value , The younger generation will be in Survivor Area to be copied many times , This can increase the survival of the younger generation Time , Increase the probability of being recycled in the younger generation This parameter is only in serial GC Only when effective .
-XX:+AggressiveOpts Speed up compilation
-XX:+UseBiasedLocking Lock mechanism performance improvement
-Xnoclassgc No garbage collection
-XX:SoftRefLRUPolicyMSPerMB Every megaheap of free space SoftReference Life time of 1s softly reachable objects will remain alive for some amount of time after the last time they were referenced. The default value is one second of lifetime per free megabyte in the heap
-XX:PretenureSizeThreshold How much more objects are allocated directly in the old generation 0 Unit byte The new generation adopts Parallel Scavenge GC When is invalid Another case of direct generation allocation is large array objects , And there is no external reference object in the array .
-XX:TLABWasteTargetPercent TLAB Occupy eden % of area 1%
-XX:+CollectGen0First FullGC Whether it's first or not YGC false

### Parallel collector related parameters

Parameter name meaning The default value is interpretative statement
-XX:+UseParallelGC Full GC use parallel MSC ( This is to be verified ) Choose garbage collector as parallel collector . This configuration is only valid for younger generation . Under the above configuration , The younger generation uses concurrent collection , The older generation still uses serial collection .( This is to be verified )
-XX:+UseParNewGC Set up the young generation to collect in parallel But with CMS Collect and use at the same time JDK5.0 above ,JVM It will be set according to the system configuration , So there's no need to set this value
-XX:ParallelGCThreads Number of threads in parallel collector This value is best configured to equal the number of processors The same applies CMS
-XX:+UseParallelOldGC Old generation garbage collection method is parallel collection (Parallel Compacting) This is JAVA 6 The parameter options that appear
-XX:MaxGCPauseMillis The longest garbage collection time of each young generation ( Maximum pause time ) If this time cannot be met ,JVM Will automatically adjust the size of the younger generation , To meet this value .
-XX:+UseAdaptiveSizePolicy Automatically select the size of the younger generation area and the corresponding Survivor Area ratio After setting this option , The parallel collector will automatically select the size of the younger generation area and the corresponding Survivor Area ratio , To achieve the minimum corresponding time or collection frequency specified by the target system , This value is recommended when using parallel collectors , Keep it open .
-XX:GCTimeRatio Set the percentage of garbage collection time in the running time of the program Formula for 1/(1+n)
-XX:+ScavengeBeforeFullGC Full GC Pre invocation YGC true Do young generation GC prior to a full GC. (Introduced in 1.4.1.)

### CMS Processor parameter settings

Parameter name meaning The default value is interpretative statement
-XX:+UseConcMarkSweepGC Use CMS Memory collection After configuring this in the test ,-XX:NewRatio=4 The configuration of has failed , Unknown cause . therefore , At this time, the size of the younger generation is the best -Xmn Set up .???
-XX:+AggressiveHeap Trying to use a lot of physical memory Optimization of long time and large memory usage , Can check computing resources （ Memory , Number of processors ） Need at least 256MB Memory a large number of CPU／ Memory , （ stay 1.4.1 stay 4CPU There is already a lift on the ）
-XX:CMSFullGCsBeforeCompaction How many times will memory compression occur Because the concurrent collector does not compress the memory space , Arrangement , So running for a while will produce " debris ", Reduce the efficiency of operation . This value sets how many times to run GC Compress the memory space later , Arrangement .
-XX:+CMSParallelRemarkEnabled Lower the mark pause
-XX+UseCMSCompactAtFullCollection stay FULL GC When , Compression of the old CMS It doesn't move memory , therefore , It's very easy to produce debris , Cause insufficient memory , therefore , Memory compression will be enabled at this time . It's a good habit to add this parameter . May affect performance , But it can eliminate debris
-XX:+UseCMSInitiatingOccupancyOnly Start with a manual definition initialization definition CMS collect prohibit hostspot Self trigger CMS GC
-XX:CMSInitiatingOccupancyFraction=70 Use cms As garbage collection Use 70％ Later on CMS collect 92 In order not to appear promotion failed( See introduction below ) error , The setting of this value needs to meet the following formula CMSInitiatingOccupancyFraction Calculation formula
-XX:CMSInitiatingPermOccupancyFraction Set up Perm Gen Use the rate reached to trigger 92
-XX:+CMSIncrementalMode Set to incremental mode Used for single CPU situation

### JVM Auxiliary information parameter setting

Parameter name meaning The default value is interpretative statement
-XX:+PrintGC Output form :[GC 118250K->113543K(130112K), 0.0094143 secs] [Full GC 121376K->10414K(130112K), 0.0650971 secs]
-XX:+PrintGCDetails Output form :[GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs] [GC [DefNew: 8614K->8614K(9088K), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs]
-XX:+PrintGCTimeStamps
-XX:+PrintGC:PrintGCTimeStamps But with -XX:+PrintGC -XX:+PrintGCDetails A mixture of Output form :11.851: [GC 98328K->93620K(130112K), 0.0082960 secs]
-XX:+PrintGCApplicationStoppedTime Time the program paused during print garbage collection . Can be mixed with the above Output form :Total time for which application threads were stopped: 0.0468229 seconds
-XX:+PrintGCApplicationConcurrentTime Print every time before garbage collection , The execution time of the program without interruption . Can be mixed with the above Output form :Application time: 0.5291524 seconds
-XX:+PrintHeapAtGC Print GC Detailed stack information before and after
-Xloggc:filename Record the relevant log information to a file for analysis . Use in combination with the above
-XX:+PrintClassHistogram garbage collects before printing the histogram.
-XX:+PrintTLAB see TLAB Use of space
XX:+PrintTenuringDistribution Check every time minor GC Threshold of new life cycle after Desired survivor size 1048576 bytes, new threshold 7 (max 15) new threshold 7 That is to say, the threshold for identifying a new life cycle is 7.

### JVM GC Garbage collector parameter settings

JVM given 3 A choice ： Serial collector Parallel collector concurrent collector . The serial collector is only suitable for small amount of data , So the choice of production environment is mainly parallel collector and concurrent collector . By default JDK5.0 It used to be a serial collector , If you want to use other collectors, you need to add corresponding parameters at startup .JDK5.0 in the future ,JVM Intelligent judgment will be made according to the current system configuration .

Serial collector

-XX:+UseSerialGC： Set up a serial collector .

Parallel collector （ Throughput priority ）

-XX:+UseParallelGC： Set to parallel collector . This configuration is only valid for younger generation . That is, the younger generation uses parallel collection , And the older generation still uses serial collection .

-XX:ParallelGCThreads=20： Configure the number of threads for the parallel collector , namely ： How many threads are garbage collected at the same time . This value is recommended for configuration with CPU The number is equal .

-XX:+UseParallelOldGC： Configure the old generation garbage collection mode as parallel collection .JDK6.0 Starting to support parallel collection of older generation .

-XX:MaxGCPauseMillis=100： Set the maximum time of garbage collection for each young generation （ Unit millisecond ）. If this time cannot be met ,JVM Will automatically adjust the size of the younger generation , To meet this time .

-XX:+UseAdaptiveSizePolicy： After setting this option , The parallel collector will automatically adjust the younger generation Eden Area size and Survivor Proportion of area size , In order to achieve the target system specified minimum response time or collection frequency and other indicators . This parameter is recommended when using a parallel collector , Keep it open .

concurrent collector （ Response time first ）

Parallel collector

-XX:+UseConcMarkSweepGC： namely CMS collect , Set up concurrent collection of elderly generation .CMS Collection is JDK1.4 Later versions began to introduce new GC Algorithm . Its main suitable scenario is that the demand for response time is more important than the demand for throughput , Able to bear garbage collection thread and application thread sharing CPU resources , And there are many long life cycle objects in the application .CMS The goal of the collection is to minimize application pause time , Reduce Full GC Probability of occurrence , Using garbage collection thread concurrent with application thread to mark and clear old generation memory .

-XX:+UseParNewGC： Set up the young generation for concurrent collection . But with CMS Collect and use at the same time .JDK5.0 above ,JVM It will be set according to the system configuration , So there is no need to set this parameter .

-XX:CMSFullGCsBeforeCompaction=0： Because the concurrent collector does not compress and organize the memory space , So running parallel collection for a while will result in memory fragmentation , Memory efficiency is reduced . This parameter is set to run 0 Time Full GC After that, compress and organize the memory space , Each time Full GC Start compressing and defragmenting memory immediately after .

-XX:+UseCMSCompactAtFullCollection： Open the compression and arrangement of memory space , stay Full GC After execution . May affect performance , But it can eliminate memory fragmentation .

-XX:+CMSIncrementalMode： Set to incremental collection mode . Generally applicable to single CPU situation .

-XX:CMSInitiatingOccupancyFraction=70： Indicates that the memory space of the old generation is used to 70% Start executing when CMS collect , To make sure that the older generation has enough space to accept objects from the younger generation , avoid Full GC Happen .

Other garbage collection parameters

-XX:+ScavengeBeforeFullGC： The younger generation GC be better than Full GC perform .

-XX:-DisableExplicitGC： Do not respond to System.gc() Code .

-XX:+UseThreadPriorities： Enable local thread priority API. Even if java.lang.Thread.setPriority() take effect , Not enabled is invalid .

-XX:SoftRefLRUPolicyMSPerMB=0： Soft reference objects can survive the last time they are accessed 0 millisecond （JVM The default is 1000 millisecond ）.

-XX:TargetSurvivorRatio=90： allow 90% Of Survivor The area is occupied （JVM The default is 50%）. Improve for Survivor Area usage .

### JVM Parameter priority

-Xmn,-XX:NewSize/-XX:MaxNewSize,-XX:NewRatio 3 Group parameters can affect the size of the younger generation , In the case of mixed use , What is the priority ？

The answer is as follows ：

High priority ：-XX:NewSize/-XX:MaxNewSize

The priority ：-Xmn（ Default equivalent -Xmn=-XX:NewSize=-XX:MaxNewSize=?）

Low priority ：-XX:NewRatio

Recommended -Xmn Parameters , The reason is that this parameter is simple , It's equivalent to setting NewSize/MaxNewSIze, And the two are equal , Suitable for production environment .-Xmn coordination -Xms/-Xmx, You can complete the heap memory layout .

-Xmn The parameter is JDK 1.4 Start supporting .

Let's use some small cases to deepen our understanding :

HelloGC yes java One after code compilation class file , Code :

public class T01_HelloGC {
public static void main(String[] args) {

for(int i=0; i<10000; i++) {
byte[] b = new byte[1024 * 1024];
}
}
}

1. java -XX:+PrintCommandLineFlags HelloGC

[[email protected] courage]# java -XX:+PrintCommandLineFlags T01_HelloGC
-XX:InitialHeapSize=61780800 -XX:MaxHeapSize=988492800 -XX:+PrintCommandLineFlags -XX
:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC

2. java -Xmn10M -Xms40M -Xmx60M -XX:+PrintCommandLineFlags -XX:+PrintGC  HelloGC
PrintGCDetails PrintGCTimeStamps PrintGCCauses


result :

-XX:InitialHeapSize=41943040 -XX:MaxHeapSize=62914560 -XX:MaxNewSize=10485760 -XX:NewSize=10485760 -XX:+PrintCommandLineFlags -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops
-XX:+UseParallelGC[GC (Allocation Failure)  7839K->392K(39936K), 0.0015452 secs]
[GC (Allocation Failure)  7720K->336K(39936K), 0.0005439 secs]
[GC (Allocation Failure)  7656K->336K(39936K), 0.0005749 secs]
[GC (Allocation Failure)  7659K->368K(39936K), 0.0005095 secs]
[GC (Allocation Failure)  7693K->336K(39936K), 0.0004385 secs]
[GC (Allocation Failure)  7662K->304K(40448K), 0.0028468 secs]
......


Command interpretation :

java: Said the use of java The actuator performs

-Xmn10M : Indicates to set the young generation value to 10M

-Xms40M : Indicates to set the minimum heap memory Heap The value is 40M

-Xmx60M : Indicates to set the maximum heap memory Heap The value is 60M

-XX:+PrintCommandLineFlags: Print explicit and implicit parameters , It's the first three lines of the result

-XX:+PrintGC : Print information about garbage collection

HelloGC : This is the startup class that needs to be executed

PrintGCDetails : Print GC Details

PrintGCTimeStamps : Print GC Time stamp

PrintGCCauses : Print GC Cause of occurrence

Interpretation of the results :

1. java -XX:+UseConcMarkSweepGC -XX:+PrintCommandLineFlags HelloGC

Said the use of CMS Garbage collector , Print parameters at the same time

Print the results :

-XX:InitialHeapSize=61780800
-XX:MaxHeapSize=988492800
-XX:MaxNewSize=329252864
-XX:MaxTenuringThreshold=6
-XX:OldPLABSize=16
-XX:+PrintCommandLineFlags
-XX:+UseCompressedClassPointers
-XX:+UseCompressedOops
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC

2. java -XX:+PrintFlagsInitial Default parameter value

3. java -XX:+PrintFlagsFinal The final parameter value

4. java -XX:+PrintFlagsFinal | grep xxx Find the corresponding parameter

5. java -XX:+PrintFlagsFinal -version |grep GC

## JVM Tuning the process

JVM tuning , There are three major aspects of design , Before the server goes wrong, we should choose the appropriate garbage processor according to the business scenario , Set different virtual machine parameters , Observation during operation GC journal , Analysis performance , Analyze the problem and locate the problem , Virtual machine debugging and so on , If the server fails , Log files should be generated in time to find the problem .

### Basic concepts before tuning

In the current garbage disposal , One is throughput first , One is response time first :

$throughput = \frac{ User code execution time }{ User code execution time + Garbage collection execution time }$

response time ：STW Shorter , The better the response time

For throughput 、 response time 、QPS、 The concept of concurrent number can be referred to : throughput （TPS）、QPS、 Concurrency number 、 response time （RT） Concept

So called tuning , First determine what to pursue , It's throughput ? Or response time ？ Or with a certain response time , How much throughput is required , wait . In general, the following areas are in pursuit of throughput : Scientific Computing 、 Data mining, etc . Throughput first garbage processor combinations are generally ：Parallel Scavenge + Parallel Old （PS + PO）.

And the businesses that pursue response time are ： Website related （JDK 1.8 after G1, Before you can ParNew + CMS + Serial Old）

### What is tuning ？

1. According to the needs JVM Planning and pre tuning
2. Optimize operation JVM Running environment （ slow , Carton ）
3. solve JVM All kinds of problems in the process of operation (OOM)

### Planning before tuning

• There is no monitoring （ Pressure test , Can see the result ）, Non tuning

• step ：

1. Familiar with business scenarios （ There is no best garbage collector , Only the right garbage collector ）

1. response time 、 Pause time [CMS G1 ZGC] （ Need to respond to users ）
2. throughput = User time /( User time + GC Time ) [PS+PO]
2. Choose the recycler combination

3. Calculate memory requirements （ Empirical value 1.5G 16G）

4. selected CPU（ The higher, the better ）

5. Set the age 、 Upgrade age

6. Set log parameters

1. -Xloggc:/opt/xxx/logs/xxx-xxx-gc-%t.log
-XX:+UseGCLogFileRotation
-XX:NumberOfGCLogFiles=5
-XX:GCLogFileSize=20M
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCCause


Log parameter explanation :

/opt/xxx/logs/xxx-xxx-gc-%t.log in XXX Said path ,%t Time stamp , It means to add a time mark to the log file , If not added , This means that the original log name will be used every time the virtual machine is started , Then it will be rewritten .

Rotation In Chinese it means cycle 、 take turns , It means this GC The log will be written circularly

GCLogFileSize=20M Specify a log size of 20M, It's too big for analysis , Too small will produce too many log files

NumberOfGCLogFiles=5 : Specify the number of logs generated

PrintGCDateStamps :PrintGCDateStamps Will print the specific time , and PrintGCTimeStamps

​ Mainly print for JVM The relative time to start , Relatively speaking, the former consumes more memory .

2. Or generate a log file every day

7. Watch the logs

Logs have analysis tools , Visual analysis tools include GCeasy and GCViewer.

### CPU High load troubleshooting process

1. System CPU often 100%, How to tune ？( Interview frequency ) CPU100% Then there must be threads occupying system resources ,

1. Find out which process cpu high （top）
2. Which thread in the process cpu high （top -Hp）
3. Export the stack of the thread (jstack)
4. Find out which way （ Stack frame ） Elapsed time (jstack)
5. The proportion of working threads is high | The proportion of garbage collection threads is high
2. System memory is soaring , How to find problems ？（ Interview frequency ）
1. Export heap memory (jmap)
2. analysis (jhat jvisualvm mat jprofiler ... )
3. How to monitor JVM
1. jstat jvisualvm jprofiler arthas top...

### CPU High load investigation cases

1. Test code ：

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
*  Read credit data from the database , Apply the model , And record and transmit the results
*/

public class T15_FullGC_Problem01 {

private static class CardInfo {
BigDecimal price = new BigDecimal(0.0);
String name = " Zhang San ";
int age = 5;
Date birthdate = new Date();

public void m() {}
}

public static void main(String[] args) throws Exception {
executor.setMaximumPoolSize(50);

for (;;){
modelFit();
}
}

private static void modelFit(){
// do something
executor.scheduleWithFixedDelay(() -> {
//do sth with info
info.m();

}, 2, 3, TimeUnit.SECONDS);
});
}

private static List<CardInfo> getAllCardInfo(){

for (int i = 0; i < 100; i++) {
CardInfo ci = new CardInfo();
}

}
}

2. java -Xms200M -Xmx200M -XX:+PrintGC com.courage.jvm.gc.T15_FullGC_Problem01

3. received CPU Alarm information （CPU Memory）

4. top The command observed the problem ： Memory keeps growing CPU High occupancy rate

[[email protected] ~]# top
top - 22:03:18 up 40 min,  5 users,  load average: 0.09, 0.16, 0.34
Tasks: 210 total,   1 running, 209 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  3.0 sy,  0.0 ni, 96.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3861300 total,  2355260 free,   904588 used,   601452 buff/cache
KiB Swap:  4063228 total,  4063228 free,        0 used.  2716336 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
3751 root      20   0 3780976  93864  11816 S  42.2  2.4   0:21.00 java
1868 mysql     20   0 1907600 357452  14744 S   0.7  9.3   0:17.40 mysqld
3816 root      20   0  162124   2352   1580 R   0.3  0.1   0:00.12 top

5. top -Hp Observe threads in process , Which thread CPU And memory

[[email protected] ~]# top -Hp 3751
top - 22:03:15 up 40 min,  5 users,  load average: 0.09, 0.16, 0.34
Threads:  66 total,   0 running,  66 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  2.5 sy,  0.0 ni, 97.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3861300 total,  2354800 free,   905048 used,   601452 buff/cache
KiB Swap:  4063228 total,  4063228 free,        0 used.  2715876 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
3801 root      20   0 3780976  93864  11816 S  1.3  2.4   0:00.40 java
3766 root      20   0 3780976  93864  11816 S  1.0  2.4   0:00.37 java
3768 root      20   0 3780976  93864  11816 S  1.0  2.4   0:00.36 java
3770 root      20   0 3780976  93864  11816 S  1.0  2.4   0:00.39 java

6. jps Location specific java process ,jstack Locate thread status

[[email protected] ~]# jstack 3751
2021-02-07 22:03:03
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.271-b09 mixed mode):

"Attach Listener" #59 daemon prio=9 os_prio=0 tid=0x00007f66bc002800 nid=0xf10 waiting on condition [0x0000000000000000]

"pool-1-thread-50" #58 prio=5 os_prio=0 tid=0x00007f66fc1de800 nid=0xee7 waiting on condition [0x00007f66e4ecd000]
at sun.misc.Unsafe.park(Native Method)
- parking to wait for  <0x00000000ff0083a0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer\$ConditionObject)
......


It should be noted that ,jstack And top -Hp Port The exported stack port number has a hexadecimal conversion relationship , for example jstack Derived " nid=0xf10 " Corresponding "3801".

For the information printed above , Focus on Waiting Relevant , Look what you're waiting for , for example :

WAITING BLOCKED eg. waiting on <0x0000000088ca3310> (a java.lang.Object)


If there is a process 100 Threads , A lot of threads are waiting on , Be sure to find out which thread holds the lock , How to find ？ Search for jstack dump Information about , See which thread holds the lock RUNNABLE.

If it's just looking at JAVA Threads , have access to jps The order focuses on ：

[[email protected] ~]# jps
4818 Jps
4746 T15_FullGC_Problem01

7. Why does Ali's standard stipulate that , The name of the thread （ Thread pools in particular ） Write meaningful names How to customize the thread name in the thread pool ？（ Customize ThreadFactory）

1. jinfo pid Process details

[[email protected] ~]# jinfo 6741
Attaching to process ID 6741, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.271-b09
Java System Properties:

java.runtime.name = Java(TM) SE Runtime Environment
java.vm.version = 25.271-b09
sun.boot.library.path = /usr/local/java/jdk1.8.0_271/jre/lib/amd64
java.vendor.url = http://java.oracle.com/
java.vm.vendor = Oracle Corporation
path.separator = :
file.encoding.pkg = sun.io
java.vm.name = Java HotSpot(TM) 64-Bit Server VM
sun.os.patch.level = unknown
sun.java.launcher = SUN_STANDARD
user.country = CN
user.dir = /usr/courage/gc/com/courage
java.vm.specification.name = Java Virtual Machine Specification
java.runtime.version = 1.8.0_271-b09
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
os.arch = amd64
java.endorsed.dirs = /usr/local/java/jdk1.8.0_271/jre/lib/endorsed
java.io.tmpdir = /tmp
line.separator =

java.vm.specification.vendor = Oracle Corporation
os.name = Linux
sun.jnu.encoding = UTF-8
java.library.path = /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/
libjava.specification.name = Java Platform API Specification
java.class.version = 52.0
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
os.version = 3.10.0-1127.el7.x86_64
user.home = /root
user.timezone =
java.awt.printerjob = sun.print.PSPrinterJob
file.encoding = UTF-8
java.specification.version = 1.8
user.name = root
java.class.path = .
java.vm.specification.version = 1.8
sun.arch.data.model = 64
sun.java.command = T15_FullGC_Problem01
java.home = /usr/local/java/jdk1.8.0_271/jre
user.language = zh
java.specification.vendor = Oracle Corporation
awt.toolkit = sun.awt.X11.XToolkit
java.vm.info = mixed mode
java.version = 1.8.0_271
java.ext.dirs = /usr/local/java/jdk1.8.0_271/jre/lib/ext:/usr/java/packages/l
ib/extsun.boot.class.path = /usr/local/java/jdk1.8.0_271/jre/lib/resources.jar:/usr
/local/java/jdk1.8.0_271/jre/lib/rt.jar:/usr/local/java/jdk1.8.0_271/jre/lib/sunrsasign.jar:/usr/local/java/jdk1.8.0_271/jre/lib/jsse.jar:/usr/local/java/jdk1.8.0_271/jre/lib/jce.jar:/usr/local/java/jdk1.8.0_271/jre/lib/charsets.jar:/usr/local/java/jdk1.8.0_271/jre/lib/jfr.jar:/usr/local/java/jdk1.8.0_271/jre/classesjava.vendor = Oracle Corporation
file.separator = /
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding = UnicodeLittle
sun.cpu.endian = little
sun.cpu.isalist =

VM Flags:
Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=209715200 -XX
:MaxHeapSize=209715200 -XX:MaxNewSize=69730304 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=69730304 -XX:OldSize=139984896 -XX:+PrintGC -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:+UseParallelGC Command line:  -Xms200M -Xmx200M -XX:+PrintGC

2. jstat -gc Dynamic observation gc situation / read GC Log discovery is frequent GC / arthas Observe / jconsole/jvisualVM/ Jprofiler（ It is best to ）

jstat gc 4655 500 : Every time 500 Millisecond print port 4655 Of GC The situation of

• S0C： The size of the first surviving area
• S1C： The size of the second surviving area
• S0U： The size of the first surviving area
• S1U： The size of the second surviving area
• EC： The size of Eden Park
• EU： The use size of Eden Park
• OC： Old age size
• OU： Old age use size
• MC： Method area size
• MU： Method area usage size
• CCSC: Compress class space size
• CCSU: Compressed class space usage size
• YGC： Garbage collection times of young generation
• YGCT： Young generation garbage collection consumes time
• FGC： Recycling times in the old days
• FGCT： Garbage collection used time in the old days
• GCT： The total time spent in garbage collection

If the interviewer asks you how to position yourself OOM The problem of ？ Can I use a graphical interface （ You can't ! Because graphical interfaces affect server performance ）

1： The system that has been launched doesn't use any graphical interface ？（cmdline arthas）

2： Where on earth is the graphical interface used ？ test ！ Monitoring during testing ！（ Piezometric observation ）

1. jmap -histo 6892 | head -10, Find out how many objects produce

 It's obvious that 1 Examples of corresponding class creation instances That's too much , In turn, track the code

1. jmap -dump:format=b,file=xxx pid ：

Online systems , Memory is very large ,jmap The execution period can have a great impact on the process , Even carton （ E-commerce is not suitable for ）

1： Set the parameters HeapDump,OOM The heap dump file will be generated automatically when the system is running

2： Many server backups （ High availability ）, Stopping this server has no effect on other servers

3： Online positioning ( Generally, small companies can't use )

[[email protected] ~]# jmap -dump:format=b,file=2021_2_8.dump 6892
Dumping heap to /root/2021_2_8.dump ...
Heap dump file created


dump File storage location :

2. java -Xms20M -Xmx20M -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError com.courage.jvm.gc.T15_FullGC_Problem01

The above meaning is to automatically generate a heap dump file when a memory overflow occurs , It should be noted that , If this file is generated, do not restart the server , Save this file and restart it .

3. Use MAT / jhat /jvisualvm Conduct dump File analysis

[[email protected] ~]# jhat -J-Xmx512M 2021_2_8.dump


The reason is that the maximum heap value set is too small , take 512M Set to 1024M Restart it :

shell
[[email protected] ~]# jhat -J-Xmx1024M 2021_2_8.dump
Dump file created Mon Feb 08 09:00:56 CST 2021
Resolving 4609885 objects...
Chasing references, expect 921 dots..........................................................
.........................................................................................Eliminating duplicate references.............................................................
......................................................................................Snapshot resolved.
Started HTTP server on port 7000


Browser input request http://192.168.182.130:7000  You can view it , Pull to the end ： Find the corresponding link   have access to OQL Find specific problem objects


Others can refer to : White ash —— software test

1. Finally, find the problem with the code

## JVM Tuning Tools

### jconsole Remote connection

1. Program start add parameters ：

java -Djava.rmi.server.hostname=192.168.182.130
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=11111
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false XXX

2. If you encounter Local host name unknown：XXX Error of , modify /etc/hosts file , hold XXX Join in

192.168.182.130 basic localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

3. close linux A firewall （ The corresponding port should be opened in actual combat ）

service iptables stop
chkconfig iptables off # Permanent ban

4. windows Open up jconsole Remote connection 192.168.182.130:11111

### jvisualvm Remote connection

This software is in JDK8 Removed in later versions , If you use it, you need to download it , And in etc/visualvm.conf Modify the default JDK_Home Address .

Reference resources : Use jvisualvm Of jstatd Remote monitoring Java Program

### Alibaba Arthas

Just look at the official website , Pure Chinese :Arthas User documentation

## JVM Tuning case

### Parameter setting is to bear the dynamic of massive access Web application

Server configuration ：8 nucleus CPU, 8G MEM, JDK 1.6.X

Parameter scheme ：

-server -Xmx3550m -Xms3550m -Xmn1256m -Xss128k -XX:SurvivorRatio=6 -XX:MaxPermSize=256m -XX:ParallelGCThreads=8 -XX:MaxTenuringThreshold=0 -XX:+UseConcMarkSweepGC

Tuning instructions ：

-Xmx And -Xms The same to avoid JVM Repeatedly reapply memory .-Xmx Is about half the size of the system's memory , That is to make full use of system resources , And give the system safe operation space .

-Xmn1256m Set the size of the younger generation to 1256MB. This value has a great influence on the system performance ,Sun The official recommendation is to configure the younger generation to be the size of the whole heap 3/8.

-Xss128k Set up a smaller thread stack to support the creation of more threads , Support mass access , And improve system performance .

-XX:SurvivorRatio=6 Set up a young generation Eden District and Survivor Area ratio . The system default is 8, According to experience, it is set as 6, be 2 individual Survivor District and 1 individual Eden The ratio of regions is 2:6, One Survivor District accounts for the whole young generation 1/8.

-XX:ParallelGCThreads=8 Configure the number of threads for the parallel collector , At the same time 8 Threads garbage collection together . This value is generally configured with CPU The number is equal .

-XX:MaxTenuringThreshold=0 Set the maximum age of garbage （ The number of lives in the young generation ）. If set to 0 Words , Then the young generation does not go through Survivor District directly into the old generation . For the older generation more applications , Can improve efficiency ; If you set this value to a larger value , The younger generation will be in Survivor Area to be copied many times , This can increase the survival time of the younger generation , Increase the probability of being recycled in the younger generation . According to the dynamics of massive access Web Features of application , Its memory is either cached to reduce direct access DB, Or it can be recycled quickly to support high concurrent mass requests , So it doesn't make sense for memory objects to survive many times in the younger generation , You can go straight to the old generation , According to the actual application effect , Set this value here to 0.

-XX:+UseConcMarkSweepGC Set up concurrent collection of elderly generation .CMS（ConcMarkSweepGC） The goal of the collection is to minimize application pause time , Reduce Full GC Probability of occurrence , Using garbage collection thread concurrent with application thread to mark and clear old generation memory , It is applicable to the situation that there are many long life cycle objects in the application .

### The internal integration of parameter setting builds the server

Application of high performance data processing tools

Server configuration ：1 nucleus CPU, 4G MEM, JDK 1.6.X

Parameter scheme ：

-server -XX:PermSize=196m -XX:MaxPermSize=196m -Xmn320m -Xms768m -Xmx1024m

Tuning instructions ：

-XX:PermSize=196m -XX:MaxPermSize=196m According to the characteristics of integration construction , Large scale system compilation may need to load a large number of Java Class into memory , So it is efficient and necessary to allocate a large amount of persistent memory in advance .

-Xmn320m Follow the younger generation size for the whole heap 3/8 principle .

-Xms768m -Xmx1024m According to the size of the heap memory that the system can roughly bear, you can .

### Case study of XXX

Analyzing , I'll sort it out in a few years ......

### Problem orientation XXX

Analyzing , I'll sort it out in a few years ......

### Virtual machine troubleshooting XXX

Analyzing , I'll sort it out in a few years ......

## JVM Tuning parameters 、 Method 、 Tools and case summary of more related articles

1. JVM Detailed explanation of tuning parameters

GC There are two types of :Scavenge GC and Full GC 1.Scavenge GC In general , When a new object is generated , And in Eden Application for space failed , It will trigger Scavenge GC, Heaped Eden Area GC, ...

2. In the production environment JVM Examples of setting tuning parameters

JVM Basics : Examples and analysis of production environment parameters The original configuration : -Xms128m -Xmx128m -XX:NewSize=64m -XX:PermSize=64m -XX:+UseConcMarkSweepGC - ...

3. turn : jvm Tuning parameters summary

JVM Inside GC(Garbage Collection) There are many kinds of algorithms , Such as mark clear collector , Compression collector , Generational collectors and so on , See HotSpot VM GC The type of Now it's more commonly used to collect by generations (generatio ...

4. 5 Kind of JVM Overview of tuning configuration methods ！！！

I arranged it for free Java Advanced information , covers Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo High concurrency distributed and so on , altogether 30G, You need to collect . Portal :h ...

5. 5 Kind of JVM Overview of tuning configuration methods

1 Heap settings -Xms: Initial heap size -Xmx: Maximum heap size -XX:NewSize=n: Set younger generation size -XX:NewRatio=n: Set the ratio of young generation to old generation . Such as : by 3, The ratio of young generation to old generation is 1:3, year ...

6. frequently-used JVM Tuning parameters summary 【 Check learning at any time 】

The parameters in this article will be updated continuously according to the parameters used later   --- (1)-Xms20M Presentation settings JVM The minimum boot memory is 20M, Must be M In units of (2)-Xmx20M Presentation settings JVM The maximum boot memory is 20M, Must be M For single ...

7. jvm tuning 、 Commonly used tools

ps -ef | grep java Find out the process id jmap -heap ID Find out jvm Configuration information Add parameters : Print Gc journal , analysis GC Log analysis tool : GCeasy Reduce minor gc and full g ...

8. JVM Tuning parameter settings ？

-Xms20M Indicates that the minimum set heap capacity is 20M, Must be M In units of -Xmx20M Indicates to set the maximum heap capacity to 20M, Must be M In units of . take -Xmx and -Xms Set to the same to avoid automatic heap expansion . Large projects -Xmx and -X ...

9. jvm Tuning parameters

Heap overflow test args:-verbose:gc -Xms20M -Xmx20M -XX:+PrintGCDetails Insufficient stack level args:-Xss128k Constant pool memory overflow args : -XX:PermS ...

10. JVM Explanation of tuning parameters

Typical configuration : java -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseP ...

## Random recommendation

1. Code First Development Series for managing concurrency and transactions

return <8 Sky master EF Of Code First Development > General catalogue This directory Understand concurrency Understand positive concurrency Understanding negative concurrency Use EF Achieve positive concurrency EF The default concurrency of Design and handle concurrent applications at the field level Realization RowVersi ...

2. Bootstrap CSS Forms

Form layout Bootstrap Provides the following types of form layouts : Vertical form ( Default ) Inline form Horizontal form Vertical or basic forms The basic form structure is Bootstrap Self contained , Individual form controls automatically receive some global styles . Here's the list ...

3. Learning notes ：MySQL Database preliminary Concept

database What is a database , Database is an efficient solution to manage a large amount of information , Organize... According to the data structure . A vehicle for storing and managing data . And we often say the database can be divided into relational and non relational database . Relational database The relational database here , yes ...

4. extract Windows User key file cachedump

extract Windows User key file cachedump   Windows The system stores the user information and key in the system archive file (System hive) And secure archiving (Security hive) in . Just extract the content , Can ...

5. Front end development and Seo Basics

Web page code optimization       1:<title> title : Emphasize the point , Key words in front , Of each page title Try to be different 2:<meta keywords> key word : List a few key words , ...

6. Android Call up a third party by broadcasting in the Internet App

Today's urgent follow-up of a Baidu video App Can't start Baidu Post App The problem of , Of course , This is only found in 4.x Of android Under the system , stay 2.x Under the version , Everything is all right ,( It's actually 3.1 There's a problem with the above version ) The specific scene is : 1. stick ...

7. DFS order Refer to Xu Haoran 《 Data structure 》

Online special talk about DFS There seems to be very few things in the order Too simple. ? Not very practical ? Read the paper 7 A classic question , I think it's very useful original text " So-called DFS order , Namely DFS A sequence of nodes visited by the whole tree in turn " &quo ...

8. VueJs Development Notes —IDE Choice and WebStorm performance optimization 、 Framework features and data call 、 Routing options and using

One .IDE The choice of : VsCode and WebStorm All good choices , Both run debugging are very convenient, you can use the shortcut key to run and stop , In terms of the speed of opening projects and the requirements for computer configuration ,vscode than webstorm A lot better ...

9. Fork And Join On the framework

First , About Fork And Join I think the best introduction to framework is : Java Threads ( 11、 ... and ):Fork/Join-Java Parallel computing framework This article refers to this article Fork/Join The core class of the framework is ForkJoin ...

10. npm install github Package mode

Directly in npm Warehouse installation npm install kiana-js --save Directly use user name and warehouse name to install npm install easterCat/kiana-js You can also add git ...