Daily sentence
In the book lies the soul of the whole past. – Carlyle
Pauses in garbage collection
The task of garbage collection is to identify and reclaim garbage objects for memory cleaning. In most cases, the system is required to come to a standstill in order for the garbage collector to perform more efficiently.
The purpose of the pause is to terminate all application threads so that no new garbage is generated in the system. At the same time, the pause ensures the consistency of the system state at a certain moment, which is also conducive to better marking of garbage objects. Therefore, application pauses occur during garbage collection.
How does the object enter the old age
Generally speaking, objects created for the first time will be placed in Eden of the new generation. If GC does not intervene, objects will not leave Eden. Then how will objects in Eden enter the old age?
Generally speaking, as long as the age of the object reaches a certain size, it will automatically leave the young generation and enter the old age. The age of the object is determined by the object going through several GC. After each GC of the new generation, if it is not collected, the age will be increased by 1.
The virtual machine provides a parameter to control the maximum age of the new generation object. When this age range is exceeded, the old age will be promoted -XX:MaxTenuringThreshold
public class Test5 {
public static void main(String[] args) {
// The initial object is in Eden
//-Xmx64m -Xms64m -XX:+PrintGCDetails
/*for(int i=0; i<5; i++){ byte[] b = new byte[1024*1024]; } * /
// Test objects into the old age
//-Xmx1024m -Xms1024m -XX:+UseSerialGC -
// XX:MaxTenuringThreshold=15 -XX:+PrintGCDetails
//-XX:+PrintHeapATGC
for(int k=0; k<20; k++){for(int j=0; j<300; j++){byte[] b=new byte[1024*1024]; }}}}Copy the code
Conclusion: The MaxTenuringThreshold parameter can be set to specify how many times a new generation object will be reclaimed before entering the old age. In addition, large objects (if the new generation of Eden cannot be loaded, it will enter the old age directly). The JVM has a parameter that can set the size of an object beyond the specified size to advance directly to the old age.
-XX:PretenureSizeThreshold
TLAB (Thread local Cache)
TLAB (Thread Local Allocation Buffer) is a memory Allocation area dedicated to threads to speed up the Allocation of objects.
Each thread generates a TLAB, a unique workspace for that thread, and the Java virtual machine uses this TLAB to avoid multithreaded conflicts. Improve the efficiency of object allocation. TLAB space is generally not large, and when large objects cannot be allocated in TLAB, they are allocated directly on the heap.
-xx :+UseTLAB uses TLAB. -xx :+TLABSize Sets the TLAB size. - XX: TLABRefillWasterFraction set maintenance into TLAB space of a single object size, it is a percentage value, the default 64, namely, if the object is more than 1/64 of the whole space, then create objects on the heap. -xx :+PrintTLAB View TLAB information -xx :ResizeTLAB Self-adjusting TLABRefillWasteFraction threshold.Copy the code
A separate block of working memory for each thread (volatile)
public class Test7 {
public static void alloc(a){
byte[] b = new byte[2];
}
public static void main(String[] args) {
/ / TLAB allocation
//-XX:+UseTLAB -XX:+PrintTLAB -XX:TLABSize=102400 -
XX:TLABRefillWasteFraction=100 -XX:-DoEscapeAnalysis
long start = System.currentTimeMillis();
for(int i=0; i<10000000; i++){ alloc(); }longend = System.currentTimeMillis(); System.out.println(end-start); }}Copy the code
Garbage collector
There is more than one garbage collector in a Java virtual machine, and we need to understand which one to use in which case and what the performance impact is.
Serial garbage collector
A serial collector is a collector that uses a single thread for garbage collection. The serial collector has only one worker thread at each collection, and its focus and exclusivity tend to perform better on computers with less parallelism. Serial recyclers can be used in both new generation and old generation. According to the different pair space, it can be divided into new generation serial collector and old serial collector.
-xx: +UseSerialGC parameter can be set to use the new generation serial collector and the old generation serial collector.
Parallel garbage collector
The parallel garbage collector is improved on the basis of serial garbage collector. It can use multiple threads to collect garbage at the same time, which can effectively shorten the actual time of garbage collection for computers with strong computing ability.
ParNew collector
- A garbage collector working in the new generation, he simply multi-threaded serial collector, its collection strategy and algorithm is the same as serial collector. -xx :+UseParNewGC The new generation uses the ParNew collector and the old generation uses the serial collector.
- The number of threads the ParNew collector works on can be specified using the -xx: ParallelGCThreads parameter. It is generally preferred to match the CPU of the computer to avoid too many threads affecting performance.
ParallelGC collector for the next generation
The collector that uses the replication algorithm is also a multithreaded collector, but ParallelGC collector has an important aspect that is very focused on throughput.
Two parameters are provided to control system throughput
-
-XX: MaxGCPauseMillis: Set the maximum garbage collection pause time, which can be used to limit the GC pause time of the virtual machine to MaxGCPauseMillis. If you want to reduce the GC pause time, you can set MaxGCPauseMillis to a small value, but this will result in frequent GC. This increases the total GC time and reduces throughput. Therefore, set the value based on the actual situation.
-
-xx: GCTimeRatio: Sets the throughput. It is an integer between 0 and 100. The default value is 99, so the system will spend no more than 1/(1+n) garbage collection time.
-
Alternatively, you can specify -xx: +UseAdaptiveSizePolicy to turn on adaptive mode, in which the size of the new generation, Eden, from/to ratio, and age of objects promoted to the old age are automatically adjusted to balance heap size, throughput, and pause time.
The old ParallelOldGC collector is also a multithreaded collector and, like the new generation ParallelGC collector, is a throughput-focused collector implemented using a mark-compression algorithm.
- -xx :+UseParallelOldGC
- -xx :+ParallelGCThreads can also set the number of threads for garbage collection.
CMS collector
CMS stands for concurrent Mark Sweep, and it uses Mark Sweep, which focuses on system pause times.
- Use -xx :+UseConcMarkSweepGC for setting
- Use -xx :+ConcGCThreads to set the number of concurrent threads
CMS is not an exclusive collector, that is, the CMS collection process, the application is still running, and new garbage is constantly generated, so in the process of using CMS, you should ensure that the application memory is sufficient. CMS will not wait for the application of saturated times of recycle garbage, but when a certain threshold to start recycling, recycling threshold can be used to specify the parameters of the configuration, – XX: CMSInitiatingOccupancyFraction to specified, the default value is 68, This means that CMS collection is performed when the space usage of the old age reaches 68%.
If the memory usage increases rapidly and the CMS is running out of memory, CMS collection will fail and the VIRTUAL machine will start the old serial collector for garbage collection, which will interrupt the application until the garbage collection is complete. The process of GC pause time may be long, so – XX: CMSInitiatingOccupancyFraction set according to the actual situation.
Before we said, when learning algorithm tag QingChuFa fragments of memory has a downside is that problem, so the CMS has a parameter Settings * * – XX: + UseCMSCompactAtFullCollection can be carried out after completion of recycle CMS defragmenter at a time, – XX: CMSFullGCsBeforeCompaction * * parameter can be set on how many times the CMS recovery after a compression on memory.
G1 collector
G1 Garbage collector (garbage-first) is a Garbage collector proposed in JDK1.7, which aims to replace CMS collector in the long term. G1 Garbage collector has a unique Garbage collection strategy. G1 is a generational Garbage collector, which distinguishes the new generation from the old generation. There are still Eden and from/to area. It does not require that the whole Eden area, the new generation and the old age space be continuous. It uses the partition algorithm.
-
Parallelism: Multiple threads can work simultaneously during G1 collection.
-
Concurrency: G1 has the ability to alternate execution with the application so that some work can be performed simultaneously with the application without completely blocking the application during the entire GC.
-
Generational GC: G1 is still a generational collector, but it works with both the new generation and the old generation. The previous garbage collector worked either in the new generation or the old generation, so this is a big difference.
-
Spatial defragmentation: G1 does not defragment after several GCS as CMS does, but reduces spatial fragmentation by effectively copying objects.
-
Predictability: Due to partitioning, G1 can only select some areas for recycling, reducing the scope of recycling and improving performance.
Use ** -xx :+UseG1GC to apply the G1 collector using -xx :MaxGCPauseMillis to specify the maximum pause time and using -xx :ParallelGCThreads** to set the number of threads to be collected in parallel
Setting Tomcat Parameters (1)
Use serial garbage collector
-XX:+PrintGCDetails -Xmx32m -Xms32m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseSerialGC
-XX:PermSize=32m
Copy the code
Test results show throughput of 871.8/ SEC 87KB/ SEC
Configure Tomcat parameters. (2) Expand memory to improve system performance
-XX:+PrintGCDetails -Xmx512m -Xms32m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseSerialGC
-XX:PermSize=32m
-Xloggc:d:/gc.log
Copy the code
The test results show that the throughput is 1383.31/ SEC 138KB/ SEC
Setting Tomcat Parameters (3)
Adjust the initial heap size
-XX:+PrintGCDetails -Xmx512m -Xms128m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseSerialGC
-XX:PermSize=32m
-Xloggc:d:/gc.log
Copy the code
The test results show that the throughput is :1501.3/ SEC 149.8KB/ SEC
(4) Test the performance of the ParNew collector
-XX:+PrintGCDetails -Xmx512m -Xms128m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseParNewGC
-XX:PermSize=32m
-Xloggc:d:/gc.log
Copy the code
The test results show that the throughput is 2130/ SEC 212KB/ SEC
Setting Tomcat Parameters (5)
Test the performance of the ParallelOldGC collector
-XX:+PrintGCDetails -Xmx512m -Xms128m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseParallelGC
-XX:+UseParallelOldGC
-XX:ParallelGCThreads=8
-XX:PermSize=32M
-Xloggc:d:/gc.log
Copy the code
The test results show that the throughput is 2236/ SEC 223KB/ SEC
Configure Tomcat parameters (5) Test the performance of the CMS collector
-XX:+PrintGCDetails -Xmx512m -Xms128m
-XX:+HeapDumpOnOutOfMemoryError
-XX:+UseConcMarkSweepGC
-XX:ConcGCThreads=8
-XX:PermSize=32M
-Xloggc:d:/gc.log
Copy the code
The test results show that the throughput is 1813/ SEC 181KB/ SEC