7 kinds of JVM garbage collectors, how many do you know?

Preface

Today we will talk about 堆内存how to use JVM 垃圾回收器for garbage collection, and how to use it 命令to configure and use these garbage collectors.

Detailed heap memory

You should already understand the above picture. You can understand that one 房子is divided into several 房间, and each room has a different function. Some are for babies, some are for parents, and some are for grandparents.

  • The heap memory is divided into 两块one block 年轻代and the other block 老年代.

  • The young generation is divided into Edenand survivor. The default ratio of their space size is 8:2,

  • The surviving area is divided into s0and s1. These two spaces are exactly the same size, they are a pair of twins, and they have a 1:1 ratio

Heap memory garbage collection process

first step

新生成The object is first placed in the Edenzone, and it 满了will trigger when the Eden zone Minor GC.

Second step

In the first step, the surviving objects in the GC will be moved to survivorthe S0 area in the area. When the S0 area is full, it will be triggered Minor GC. The surviving objects in the S0 area will be moved to the S1 area, and the S0 area will be free.

After S1 is full, in the GC, the surviving ones move to the S0 area again, and the S1 area is free. In this way, the GC is repeated. After each GC, the age of the object 涨一岁reaches a certain value (15), and it will enter 老年代.

third step

After it happens once Minor GC(prerequisite), the old generation may appear Major GC, depending on the garbage collector.

Full GC trigger conditions

  • Manually call System.gc, it will continuously execute Full GC

  • Insufficient/full space in the old age

  • Insufficient/full method area

note

We need to remember a word: stop-the-world. It will happen in any GC algorithm. stop-the-world means the execution of the 停止application because the JVM needs to perform GC .

When stop-the-world occurs, all but the threads required for GC 线程enter the 等待state until the GC task is completed. GC optimization is often to reduce the occurrence of stop-the-world.

Which areas are objects to be recycled

It should be noted that JVM GC only reclaims 堆内存and 方法区内objects. And 栈内存data in the JVM goes out of scope is automatically freed, so it is not within the management range of the GC JVM.

Common parameter configuration of heap memory

parameter description
-Xms Initial size of heap memory, unit m, g
-Xmx The maximum allowable size of heap memory, generally not greater than 80% of physical memory
-XX:PermSize The initial size of non-heap memory, general application settings are initialized to 200m, and the maximum size is 1024m.
-XX:MaxPermSize Maximum allowable size of non-heap memory
-XX:NewSize(-Xns) Initial memory size of the young generation
-XX:MaxNewSize(-Xmn) Maximum allowable memory size of the young generation
-XX:SurvivorRatio=8 The ratio of the capacity of the Eden area to the Survivor area in the young generation, the default is 8, which is 8:1
-Xss Stack memory size
-XX:NewRatio=old generation/new generation Set the size ratio of the old and young generations
-XX:+PrintGC After the jvm is started, it will print the log whenever it encounters GC
-XX:+PrintGCDetails View GC detailed information, including the situation of each district
-XX:MaxDirectMemorySize You can directly access "direct memory" in NIO , this is to set its size, if not set, the default is the value of the maximum heap space-Xmx
-XX:+DisableExplicitGC Close System.gc()
-XX:MaxTenuringThreshold Garbage can enter the old age
-Xnoclassgc Disable garbage collection
-XX:TLABWasteTargetPercent The percentage of TLAB in eden area, the default is 1%
-XX:+CollectGen0First YGC first when FullGC, default false

TLAB memory

The full name of TLAB is Thread Local Allocation Buffer, that is 线程本地分配缓存, from the name, it is a thread-specific memory allocation area, which is born to accelerate object allocation.

Each thread will generate a TLAB, the thread's exclusive work area, the Java virtual machine uses this TLAB area to avoid multi-thread conflicts and improve the efficiency of object allocation.

TLAB space is generally not too large. When large objects cannot be allocated in TLAB, they will be directly allocated on the heap.

parameter description
-Xx:+UseTLAB Use TLAB
-XX:+TLABSize Set TLAB size
-XX:TLABRefillWasteFraction Set and maintain the size of a single object that enters the TLAB space. It is a scale value and the default is 64, that is, if the object is larger than 1/64 of the entire space, it will be created in the heap
-XX:+PrintTLAB View TLAB information
-Xx:ResizeTLAB Self-adjusting TLABRefillWasteFraction threshold.

Overview of the Garbage Collector

New generation configurable collectors: Serial, ParNew, Parallel Scavenge

Recyclers configured in the old age: CMS, Serial Old, Parallel Old

The connection between the collectors of the new generation and the old generation area indicates that they can be used together.

New Generation Garbage Collector

Serial garbage collector

Serial collector is the most basic collector with the longest development history. Commonly known as:, 串行回收器used 复制算法for garbage collection

Features

Serial collector refers to a collector that uses a single thread for garbage collection. The serial collector has only one worker thread for each collection.

For single-CPU computers with weak parallelism, the concentration and exclusivity of serial collectors tend to have better performance.

It has Stop The World problem, and when garbage collection, the program must be stopped.

Use -XX:+UseSerialGCparameters to set the new generation to use this serial collector

ParNew garbage collector

ParNew is actually the 多线程version of Serial . Except for multithreading, the rest of the parameters are exactly the same as Serial. Commonly known as:, 并行垃圾回收器used 复制算法for garbage collection

Features

The number of threads opened by ParNew by default is the same as the number of CPUs. On machines with a large number of CPU cores, the number of -XX:ParallelGCThreadsthreads can be set by parameters .

It is currently the preferred garbage collector for the new generation, because apart from ParNew, it is the only one that can work with the old CMS.

It also has the Stop The World problem

Use -XX:+UseParNewGCparameters to set the new generation to use this parallel collector

ParallelGC Collector

ParallelGC uses a replication algorithm to recycle garbage and is also multi-threaded.

Features

Is very concerned about the throughput of the system, 吞吐量= 代码运行时间/( 代码运行时间+ 垃圾收集时间)

-XX:MaxGCPauseMillis: Sets the maximum garbage collection pause time, the virtual machine can be used in the GC pause time control in MaxGCPauseMillis range, very small if you want to reduce the GC pause times can be set MaxGCPauseMillis, but will result GC频繁, thereby increasing the GC 总时间, 降低the 吞吐量. So you need to set this value according to the actual situation.

-Xx:GCTimeRatio: Set the throughput size, which is an integer between 0 and 100. By default, its value is 99, then the system will spend no more than 1/(1+n)time for garbage collection, that is 1/(1+99)=1%, time.

In addition, you can specify to -XX:+UseAdaptiveSizePolicyturn on the adaptive mode. In this mode, the size of the young generation, the ratio of eden, from/to, and the age of objects promoted to the old generation will be automatically adjusted to achieve the heap size, throughput and The balance point between pause times.

Use the -XX:+UseParallelGC parameter to set the new generation to use this parallel collector

Old age garbage collector

SerialOld garbage collector

SerialOld is the 老年代collector version of the Serial collector, and it is also a 单线程collector.

use

  • One is used in conjunction with the Parallel Scavenge collector in JDK1.5 and earlier versions,

  • The other is as a backup plan for the CMS collector. If the CMS has a Concurrent Mode Failure, SerialOld will act as a backup collector.

使用算法: Marking-sorting algorithm

ParallelOldGC Collector

The old-generation ParallelOldGCcollector is also a multi-threaded collector. Like the new-generation ParallelGC collector, it is also a collector that focuses on throughput. It uses it 标记压缩算法for implementation.

-XX:+UseParallelOldGcSet the old age to use the collector

-XX:+ParallelGCThreadsYou can also set the number of threads during garbage collection.

CMS collector

The full name of CMS: Concurrent Mark Sweep means Concurrent Mark Sweep, which he uses 标记清除法. Mainly focus on the system pause time.

Use -XX:+UseConcMarkSweepGCto set the old generation to use the collector.

Use to -XX:ConcGCThreadsset the number of concurrent threads.

Features

CMS is not an exclusive collector, that is to say, during the process of CMS recycling, the application is still working continuously, and new garbage will continue to be generated. Therefore, you should ensure that the memory of the application is sufficient when using the CMS. Available.

CMS will not wait until the application 饱和to recycle garbage, but start recycling at a certain threshold. The recycling threshold can be configured with specified parameters: -XX:CMSInitiatingoccupancyFractionto specify, the default is 68, that is, when the space 使用率of the old generation reaches 68%At the time, it will be 执行recycled by CMS.

If the memory usage increases very quickly, there is already insufficient memory during the execution of the CMS. At this time, the CMS recycling will fail, and the virtual machine will start the old-generation 串行collector; SerialOldGCgarbage collection will cause the application Interrupted, it will not work until the garbage collection is complete.

The GC pause time in this process may be longer, so -XX:CMSInitiatingoccupancyFractionthe setting should be based on the actual situation.

One disadvantage of the mark-sweeping method is the existing 内存碎片problem, so CMS has a parameter setting that -XX:+UseCMSCompactAtFullCollecioncan be performed once after the CMS recovery is completed 碎片整理.

-XX:CMSFullGCsBeforeCompactionThe parameter can set how many times CMS recycles, and then the memory is executed once 压缩.

G1 collector

The length is too long, we will explain in the next article. In addition, you can follow the public number Java technology stack and reply jvm46 to get a JVM tuning guide.

Recent hot articles:

1. I wrote a piece of logic in Java 8, but my colleagues couldn't understand it directly

2. Spring Boot study notes, this is too complete!

3. Hang Tomcat, Undertow performance is very explosive! !

4. Spring Boot is too cruel, release 3 versions at a time!

5. How does Spring Boot integrate Redis quickly?

6、The latest release of "Java Development Manual (Songshan Edition)"

7. Spring Boot Redis implements distributed locks, which is so fragrant!

8. Chinese open sourced a small and complete Java tool library !

9. The Chinese open sourced a super easy to use Redis client! !

10、My colleague wrote a hidden bug, and I checked it for 3 days!

Scan the QR code to follow the official account of the Java Technology Stack to read more dry goods.

Click " Read Original " to get a complete list of interview questions~

Guess you like

Origin blog.csdn.net/youanyyou/article/details/108413780