当前位置:网站首页>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 .
边栏推荐
- 【笔记】再笔记--边干边学Verilog HDL – 014
- Who took advantage of the chaos and looted in Tiktok Wenwan?
- 洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
- 3-3主機發現-四層發現
- 习题8 #第8章 Verilog有限状态机设计-4 #Verilog #Quartus #modelsim
- 75.二叉搜索树额最近公共祖先
- PHP implementation of sorting two-dimensional arrays by specified key names
- 数据库是什么?数据库详细笔记!带你走进数据库~你想知道的这里都有!
- Several code screenshots beautification tools worth collecting by programmers
- AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
猜你喜欢

企业数字化转型的点、线、面、体!

4-2端口Banner信息获取

洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台

Exploration and practice of NLP problem modeling scheme

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

Advanced features of selenium webdriver

【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace

CAD Assistant - 3D模型格式转换利器

3-2主机发现-三层发现
![[software testing] 01 -- software life cycle and software development model](/img/57/a4fcb40a33c80b0bcf2187d1943abd.jpg)
[software testing] 01 -- software life cycle and software development model
随机推荐
startService() 过程
销量赶不上拿钱速度,威马赴港救急
物理验证LVS流程和技术点滴(上)
做白银k线图有多重要?
揭秘!付费会员制下的那些小心机!
习题8 #第8章 Verilog有限状态机设计-4 #Verilog #Quartus #modelsim
75. nearest common ancestor of binary search tree
PHP outputs all dates between two specified dates
Automatically obtain local connection and network address modification
誰在抖音文玩裏趁亂打劫?
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
NLP 类问题建模方案探索实践
企业数字化转型的点、线、面、体!
Win11安装权限在哪里设置?Win11安装权限设置的方法
PHP实现二维数组按指定的键名排序的方法
【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...
Rejected by a large company? Tencent experts summarized 11 reasons for being rejected!
深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)
Freeswitch dial extension
数据基础设施升级窗口下,AI 新引擎的技术方法论