当前位置:网站首页>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 .
边栏推荐
- Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
- Product axure9 (English version), repeater implements addition, deletion, query and modification of table contents (crud)
- MariaDB的安装与配置
- 为什么信息化 ≠ 数字化?终于有人讲明白了
- 测试方法学习
- 75.二叉搜索树额最近公共祖先
- docker compose 部署Flask项目并构建redis服务
- The sales volume could not catch up with the speed of taking money. Weima went to Hong Kong for emergency rescue
- Who took advantage of the chaos and looted in Tiktok Wenwan?
- ArrayList<Integer>使用==比较值是否相等出现 -129!=-129的情况思考
猜你喜欢

做白银k线图有多重要?

From CIO to Consultant: the transformation of it leaders

76. nearest common ancestor of binary tree

After CDN is added to the website, the Font Icon reports an error access control allow origin

Advanced features of selenium webdriver

JVM(2) 垃圾回收

Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)

以其他组件为代价的性能提升不是好提升

从CIO到顾问:IT领导者的转型之路

正则表达式系列之手机号码正则
随机推荐
PHP outputs all dates between two specified dates
物理验证LVS流程和技术点滴(上)
KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
Solution of portable emergency communication system for flood control and rescue
数据基础设施升级窗口下,AI 新引擎的技术方法论
Technical methodology of new AI engine under the data infrastructure upgrade window
网站加了CDN后,字体图标报错Access-Control-Allow-Origin
Installation and configuration of MariaDB
3-2主机发现-三层发现
Have you mastered all the testing methods of technology to ensure quality and software testing?
76. nearest common ancestor of binary tree
高能直播,大咖云集!邀你共启BizDevOps探索之路。
一小时构建示例场景 声网发布灵隼物联网云平台
Automatically obtain local connection and network address modification
福昕软件受邀亮相2022先进制造业数智发展论坛
小米笔试真题一
KDD 2022 | 协同过滤中考虑表征对齐和均匀性
With these four security testing tools, software security testing can be said so easy!
测试方法学习
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践