当前位置:网站首页>JVM (2) garbage collection
JVM (2) garbage collection
2022-06-29 19:21:00 【Demon_ bh】
Garbage collection
1、 How to judge whether the object is garbage
- Reference counting ( There is a reference to this object , Counting plus one 《
There are circular references》) - Reachability analysis algorithm ( To determine whether or not Root object GC Root Direct or indirect reference to 《
Grape kebabs》) - It can be used as GC Root The object of
- Virtual machine stack ( Local variables in stack frames ) Object referenced in .
- Object referenced by a class static property in a method area
- The object referenced by a constant in the method area
- Local method stack JNI( In other words Native Method ) Referenced object
Four quotations :
- Strong citation
- Soft citation ( After garbage collection, soft reference objects will be recycled again when memory is still insufficient )
- Weak reference ( Garbage collection , Will recycle weak reference objects )
- Virtual reference ( Mainly with ByteBuffer Use , Free direct memory )
2、 Garbage collection algorithm
1) Mark clear
Definition :Mark Sweep
- Faster
- Memory fragmentation will occur
2) Tag to sort out
Mark Compact
- Slow speed
- No memory fragmentation
3) Copy ( The new generation —>98% It's all life and death )
Copy
- There will be no memory fragmentation
- It takes up twice the memory space
4) Generational recycling

- The newly created object is first assigned to eden District
- When the new generation is short of space , Trigger minor gc ,eden District and from Use of live objects - copy Copied to the to in , Living objects Life plus one , Then exchange from to
- minor gc May trigger stop the world, Pause other threads , After garbage collection , Resume user thread operation
- When the object in the surviving area Life exceeds the threshold 15 , Will be promoted to the old age
- When the old days were short of space , Will trigger minor gc, If space is still insufficient , Then trigger full gc ,stop the world Longer time !
3、 Garbage collector
1) Serial The collector ( Serial )
characteristic : Single thread 、 A simple and efficient ( Compared with the single thread of other collectors ), Using replication algorithm . For a limited single CPU In terms of environment ,Serial The collector has no overhead of thread interaction , Focusing on garbage collection can naturally achieve the highest single thread collection efficiency . When the collector is recycling , All other worker threads must be suspended , Until it's over (Stop The World)!
2) Parallel GC The collector ( High throughput )
characteristic : Belong to the new generation collector is also the collector using replication algorithm ( Used the surviving areas of the Cenozoic ), again parallel The multithreaded collector of ( And ParNew Collector similar )
The goal of this collector is to achieve a controllable throughput . There is also a point worthy of attention :GC Adaptive adjustment strategy GCTimeRatio( And ParNew The most important difference between collectors )
3)CMS The collector ( Low latency )

Concurrent Mark Sweep, A method aiming at obtaining the shortest recovery pause time Old age collector
characteristic : be based on Mark - eliminate Algorithm implementation . Concurrent collection 、 Low pause , But will Generate memory fragmentation
CMS The collector runs as follows 4 Step :
- Initial marker Mark GC Roots Objects that can be reached directly
- Concurrent Tags Conduct GC Roots Tracing The process of
- Re label ( Long time ) In order to fix the tag record of the part of the object whose tag changes because the user program continues to run during the concurrent tag ==( Scan the entire heap memory )==
- Concurrent elimination Clean up and recycle marked objects , In the process of clearing , There may still be new waste , This garbage is called floating garbage , If the user needs to store a large object , The new generation can't let go , In the old days, there was too much floating garbage , Will degenerate into serial Old The collector
CMS Collectors have three disadvantages :
- Yes CPU Resource sensitive ( Complicated with common diseases )
- Can't handle floating garbage —> Degenerate to serial Old The collector
- Mark Sweep —> Generate memory fragmentation —> Full gc
CMS The collector's memory recovery process is executed concurrently with the user thread , Collocation ParNew The collector ( Multithreading , The new generation , Copy algorithm ) And Serial Old The collector ( Single thread , Old age , Mark - Sorting algorithm ) Use .
4)G1 (First) The collector ( Low latency )

characteristic : A change in thought .G1 Will heap memory “ break up the whole into parts ”, Divide the heap memory into separate areas of equal size Region.( Make it KPI( Require at any time 1 In seconds ,STW The pause shall not exceed 10ms).G1 Will try to achieve this goal , It can calculate the general area of this collection in reverse , Complete the collection incrementally .)
- No memory fragmentation ( Mark - Arrangement + Partial view replication )
- Predictable pauses
- establish Region List of recycling values , Every time == Priority recovery is the most valuable (First)==Region
- Every Region Corresponding to one Remembered Set:
- Yes Region Reference type data write operation in , produce Write Barrier barrier , Check whether the reference object is in another Region in :
- if , Through the card watch CartTable Record reference information to the referenced object Region Of Remembered Set in
- To avoid When object references are different Region Full heap scan of objects
- Yes Region Reference type data write operation in , produce Write Barrier barrier , Check whether the reference object is in another Region in :
G1 The operation process of is similar to CMS broadly , There are four steps :
- Initial marker Mark only GC Roots Objects that can be directly related to
- Concurrent Tags GC Root Start the reachability analysis of objects in the heap
- Final marker A brief treatment of the remains of Missing label object
- Screening and recovery : each Region Sorted by recovery value and cost , Make a recycling plan based on the user's expected pause time , Sure Choose as many as you like Region Make up a recycling set , And then the part that decided to recycle Region Of live objects copied to empty Region in , And clean up the whole old Region The whole space of . The operation here involves moving the living object , It is necessary to pause the user thread , Done in parallel by multiple collector threads .
4、GC tuning
Tuning ideas :
- Choose the right garbage collector
- Consider whether unnecessary objects are loaded into memory
- Is there a memory leak ( soft 、 Weak reference , cache )
- The priority is to start in the Cenozoic GC( Increase the proportion of Cenozoic generation 25%-50%)
- The new generation time is mainly spent in replication
Tuning case :
- GC frequent : enlarge New generation memory , Raise the promotion threshold ;
- (CMS) A single GC A long pause : Recycle once in Cenozoic before re marking ;
- (jdk7) Old age is abundant FullGC: Increase permanent generation .
边栏推荐
- php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
- Cross browser testing of selenium
- 【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...
- SQL Server Backup and restore command operations
- The concept and properties of mba-day26 number
- 【Proteus仿真】矩阵键盘中断扫描
- 七牛图片访问参数(缩略图小尺寸啥的,避免浪费流量)
- 福昕软件受邀亮相2022先进制造业数智发展论坛
- layer.prompt
- Solution of portable emergency communication system for flood control and rescue
猜你喜欢

JVM(2) 垃圾回收

3-3主機發現-四層發現

What about frequent network disconnection of win11 system? Solution to win11 network instability

电脑ssd硬盘怎么安装使用

Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines

75.二叉搜索树额最近公共祖先

【软件测试】01 -- 软件生命周期、软件开发模型

Point, line, surface and body of enterprise digital transformation!

习题8 #第8章 Verilog有限状态机设计-4 #Verilog #Quartus #modelsim

AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
随机推荐
Oracle11.2.0.4-Rac集群hang分析记录
七牛图片访问参数(缩略图小尺寸啥的,避免浪费流量)
startService() 过程
JS judge whether the array key name exists
构建增强现实移动应用程序的六款顶级工具
打新债线上开户安全吗
Flutter 调用百度地图APP实现位置搜索、路线规划
Automatically obtain local connection and network address modification
How is the combination of convolution and transformer optimal?
谁在抖音文玩里趁乱打劫?
Have you mastered all the testing methods of technology to ensure quality and software testing?
Cantata 9.5版本已正式通过SGS-TÜV认证,符合所有主要软件安全标准
自动获取本地连接及网络地址修改
swift可选值总结
4-2 port banner information acquisition
74. maximum profit on shares
Qui vole dans un jeu d'écriture?
How to install and use computer SSD hard disk
Where is the win11 installation permission set? Win11 installation permission setting method
Unittest unit test framework