当前位置:网站首页>10 minutes to understand CMS garbage collector in JVM
10 minutes to understand CMS garbage collector in JVM
2022-07-02 04:03:00 【Java architecture design】
CMS It's based on markers - Clearing algorithm's , The collection is divided into 4 A step :
Initial marker
Concurrent Tags
Re label
Concurrent elimination
Initial marker
The initial mark is just a mark GC Roots Objects that can be directly related to , So it's very fast . For example, below , This way GC Roots Using the virtual machine stack as an example . Two VMS stack tables create objects OBJ_A1 and OBJ_B1, They also have their own other quotes , At this stage , He just marks OBJ_A1 and OBJ_B1, Other references are unmarked , So even though this stage has STW, But the number of markers is small , Time soon , It doesn't affect .
Concurrent Tags
Concurrent marking is based on the object directly or indirectly referenced by the initial marked object , For example, the figure below is right OBJ_A2,OBJ_AN Mark concurrency . There's no such thing at this stage STW, So you can create objects , Add a new reference , It also causes some objects to lose references , For example, below ,OBJ_B1 It has become rubbish ,OBJ_C1 It's a new live object . This stage is due to tracking all the objects of the old age , So it's very time consuming .
Re label
In concurrent tags , We see living objects OBJ_C1 And garbage objects OBJ_B1 Wait is not marked , So this stage is to re label these objects . There are also STW, However, only objects with changes in concurrent tags are marked , These numbers are relatively small , So the speed is also very fast .
Concurrent elimination
This stage , It's just after the tagging , Cleaning up garbage objects , Just like concurrent tags , It's time consuming , Because there is no STW, So it has little effect on the operation of the program .CMS It uses the marking and clearing algorithm .
shortcoming
CMS Of 4 Stages , Initial marking and relabeling require STW, But the time is short , The impact is not big . Concurrent marking and concurrent cleanup do not require STW, Although it takes time , But concurrent execution , It doesn't matter much , look CMS Perfect. , But he also has some shortcomings .
CPU
CMS The number of recycle threads started by default is (CPU Number +3)/ 4, That is to say CPU stay 4 More than one time , The garbage collection thread shall not be less than 25% Of CPU resources , And with CPU An increase in quantity leads to a decrease . For example, the server is 2 nucleus 4G, Then we need to use (2+3)/4=1 Threads to handle concurrent tagging and concurrent cleanup , At this time, only 1 Threads handle other things .
Floating garbage
The production of floating garbage , Mainly in the concurrent cleanup phase . After re labeling ,CMS The garbage collector will know what needs to be cleaned up , In the concurrent cleanup phase , Clean up the re marked garbage objects , There's no such thing at this stage STW, So it's possible to create new objects .
For example, in the picture below OBJ_N, After you create , Stack frames are recycled , The quotation is gone , He can't be cleared at this stage , I can only wait for the next garbage collection , Marked and cleared .
If the objects in the old age of this stage exceed the remaining space , Will appear Concurrent Mode Failure Failure , The virtual opportunity is temporarily enabled Serial Old Collector for old age garbage collection .
It can be used XX:CMSInitiatingOccupancyFraction Set the percentage of old age space occupied CMS Recycling ,JDK1.6 After the default 92%.
space debris
stay 《JVM Garbage collection algorithm 》 It is mentioned in the article , Mark - The clean-up algorithm will generate space debris , If there is not enough contiguous memory space to hold objects that are about to enter the old age , This will trigger Full GC.
To avoid that ,CMS The collector provides a -XX:+UseCMSCompactAtFullCollection, Open by default , When Full GC After completion , He will STW, To organize the memory , Keep the living objects together , Make room for continuity .
If you have to reset the memory every time , The city STW, therefore CMS It also provides -XX:CMSFullGCsBeforeCompaction Parameters , The default is 0, Indicates how many times Full GC After finishing the memory .
Link to the original text :https://mp.weixin.qq.com/s?__biz=Mzg4MjU0OTM1OA==&mid=2247499410&idx=1&sn=787037527afd5762e407626d010a7589
边栏推荐
- Vite: scaffold assembly
- 0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed
- Hand tear - sort
- 【力扣刷题】15.三数之和(双指针);17.电话号码的字母组合(递归回溯)
- Use of go package
- The 9th Blue Bridge Cup single chip microcomputer provincial competition
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- 【leetcode】34. Find the first and last positions of elements in a sorted array
- Which insurance company has a better product of anti-cancer insurance?
- SQL: common SQL commands
猜你喜欢
蓝桥杯单片机省赛第五届
Force buckle 540 A single element in an ordered array
【小技巧】使用matlab GUI以对话框模式读取文件
The 6th Blue Bridge Cup single chip microcomputer provincial competition
Failed to upgrade schema, error: “file does not exist
Influence of air resistance on the trajectory of table tennis
Get started with Aurora 8b/10b IP core in one day (5) -- learn from the official routine of framing interface
Flutter中深入了解MaterialApp,常用属性解析
The second game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
随机推荐
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
go 变量与常量
Hands on deep learning (II) -- multi layer perceptron
Raspberry pie GPIO pin controls traffic light and buzzer
Go language introduction
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
Visual slam Lecture 3 -- Lie groups and Lie Algebras
u本位合约爆仓清算解决方案建议
Vite: scaffold assembly
[JS -- map string]
接口调试工具模拟Post上传文件——ApiPost
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
《动手学深度学习》(二)-- 多层感知机
Where can I buy cancer insurance? Which product is better?
MySQL advanced SQL statement 2
Vite: configure IP access
L'avènement de l'ère 5G, une brève discussion sur la vie passée et présente des communications mobiles
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
How to solve the problem that objects cannot be deleted in Editor Mode
Opencv learning example code 3.2.4 LUT