当前位置:网站首页>Garbage first of JVM garbage collector
Garbage first of JVM garbage collector
2022-07-06 17:16:00 【TiggerRun】
Garbage collector
Note navigation
Garbage First GC
Record JVM Learning notes , Understanding may be biased , Hope to point out .
G1 Garbage collector
key word :Region、 Partition algorithm 、 The delay is controllable 、 Pause prediction model 、 parallel 、 Concurrent 、RSet、MixedGC
G1 What we hope to achieve : Improve throughput as much as possible with controllable delay
summary
- G1 It's a
parallel 、 ConcurrentDual garbage collector , Divide the memory into many irrelevant areas (RegionPhysically discontinuous ) - G1 Avoid the whole heap in a planned way GC,
Each time according to the allowed collection time , Give priority to collecting the most valuable Region - G1 Facing the server application , It is mainly equipped with
Multicore CPU And large memory machines - G1 stay JDK7 Officially enabled in ,JDK9 The default garbage collector after
Memory allocation policy
stay G1 Two memory allocation strategies are used in the garbage collector at the same time
- Pointer collision : In a Region Allocate memory for new objects through pointer collision
- Free list : Maintain a LinkedList To save free Region
Keep the idea of generational collection
- G1 Still keep the idea of generations , Or divide the heap memory into
The new generation、Old age - The memory distribution is no longer fixed ,G1 Divide memory into multiple
Region - Region Sure
dynamicPlay different roles (The new generation、Old age、Humongous), More than one size Region Objects of will be stored in continuous HumongousRegion in , Usually, it will be treated as the elderly generation .

Spatial integration
- G1 There is defragmentation ,G1 Memory reclamation of Region In units of ,Region Between
Copy algorithm, On the whole, it can be regarded as a label compression algorithm .
Pause prediction statistical model
G1 adopt The mean attenuation For each Region Time consumption of recycling 、 Calculate the amount of garbage and other measurable costs to obtain a series of statistical values . The mean value of attenuation can be expressed Current Average state , Use statistics to predict that garbage collection will start now , Which one Region There will be better benefits . Maintain a Priority list , So it's called Garbage First Recycling benefits first Region.
Solutions for cross region references

Card table
Card table (CardTable), By memory card (Card) constitute
every last Region The default in accordance with the 512kb It is divided into many cards to form a card table ,RSet It records the location of the card, not the object itself
Memory set
Memory set (Remember Set,R Set), Is to solve cross Region Object reference of , stay YoungGC when , If an object is referenced by an older object , Should not be cleared , But because of G1 The heap memory of the collector is partitioned , You need to traverse the whole heap to get the reference relationship of the object , stay YGC It is unreasonable to traverse the entire heap memory .
every last Region All assigned a RSet, Stored the reference relationship of the object (Region1 Of Region1Card_x Refer to the Region2 Of Region2Card_y), After having such a reference relationship , Reachability analysis only needs to obtain GC Roots, There is no need to traverse the entire heap space
Detailed explanation of recycling stage
YOUNG GC Stage
- Root scan ( Initialization tag )
- Update dirty card queue ( Maybe the reference relationship of the card was modified during the code execution ), to update Rset( Sync ), This ensures what you get Rset The reference relationship of is up-to-date
- Handle Rset, Identify those cited by older generations Eden District 、S Objects in the zone , That is, tell the garbage collector that this object is not garbage
copy object,Eden Objects that live in the area will be copied to S Free area of zone , Age goes up , Those who reach the threshold enter the elderly area , Some objects will be promoted to the elderly area( Because it's a replication algorithm ,G1 Realize defragmentation )- Handling references , Caused by copying objects Region change , So update Rset
Concurrent tagging phase
The process of marking garbage is time-consuming, because it needs to traverse the reference relationship , But this process is executed concurrently , Not at all STW,GC Threads and user threads work at the same time .
- Concurrent Tags : If you find that Region It's all garbage and will be carried out directly
Real time recyclingInstead of waiting for the next stage . - Mark again (
STW): Because the process of concurrent marking is concurrent , The user thread may have modified the reference relationship , Therefore, the marking needs to be corrected . - Exclusive cleanup (
STW): Calculate each Region Recovery ratio of , And sort it out , Prepare for the next phase - Concurrent cleanup : Identify and completely clean up idle Region
MIXED GC Stage
- In this stage , rate of recovery 100% Of Region It has been cleared in real time , Only some garbage is left Region
- When the heap memory usage reaches a certain threshold
-XX:G1MixedGCLiveThresholdPercentafter , Automatic mixing and recycling - distinguish MixedGC and FullGC,MixedGC Only part of the old age is recycled
- Old garbage memory segmentation will be divided 8 Time
-XX:G1MixedGCCountTargetTo recycle , Those with high recycling ratio will be recycled first - Of course, it is not necessarily implemented 8 Time , because
-XX:G1HeapWastePercentSpecifies the allowable waste space ratio
G1 The defects that exist
- Extra memory space
- Additional execution load
G1 The applicable scenarios of
- The first is the server application 、 Large memory 、 many CPU processor
- Application requirements GC It's low latency , The pause time is controllable
- Can be substituted for CMS
Be careful
The pause time is not as short as possible, If the pause is too short , It may result in only a few being processed at a time Region, Object generation is faster than garbage disposal , It will eventually lead to FullGC
Common parameters
| Parameter name | The default value is | effect |
|---|---|---|
| -XX:+UseG1GC | JDK9 Later versions use G1 | start-up G1 Garbage collector |
| -XX:G1HeapRegionSize | 1m | Set up Region Size (1m~32m Must be 2 The power of ) |
| -XX:MaxGCPauseMillis | 200ms | Set the maximum pause time |
| -XX:ParallelGCThreads | 8 | Number of parallel threads |
| -XX:ConcGCThreads | 2 | Number of concurrent threads , recommend 1/4 Parallel thread |
| -XX:InitiatingHeapOccupancyPercent | 45% | Set the memory usage threshold that triggers the concurrency flag |
| -XX:G1MixedGCCountTarget | 8 | MixedGC The stage is completed in several times |
| -XX:G1MixedGCLiveThresholdPercent | 65% | Set trigger MixedGC Heap memory usage threshold |
| -XX:G1HeapWastePercent | 10% | Set the allowable heap memory waste |
Optimization Suggestions
Young generation size
Use G1 Garbage collector , Don't passXmn、-XX:NewRatioSet a fixed younger generation size , It is to hand over to G1 Garbage collector dynamic settings , Otherwise, the target pause time will be overwritten .The pause time should not be set too small
边栏推荐
猜你喜欢

Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021

服务器端渲染(SSR)和客户端渲染(CSR)的区别

TCP's three handshakes and four waves

汇编语言段定义

JVM之垃圾回收器下篇

Description of project structure configuration of idea

Ruoyi-Cloud 踩坑的BUG

Logical operation instruction

吴军三部曲见识(七) 商业的本质

Prototype chain inheritance
随机推荐
Mongodb learning notes
Koa Middleware
MySQL字符串函数
JVM 垃圾回收器之Garbage First
JVM garbage collection overview
The difference between URI and URL
汇编语言段定义
vscode
面试集锦库
JVM运行时数据区之程序计数器
Control transfer instruction
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
Description of project structure configuration of idea
js垃圾回收机制和内存泄漏
Logical operation instruction
JVM类加载子系统
Activit零零碎碎要人命的坑
Yum install XXX reports an error
Train 100 pictures for 1 hour, and the style of the photos changes at will. There is a demo at the end of the article | siggraph 2021
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。