当前位置:网站首页>JVM garbage collection mechanism
JVM garbage collection mechanism
2022-06-27 06:04:00 【The season when the monsoon dies】
One 、 Methods for judging garbage objects
public class ReferenceCountingGc {
Object instance = null;
public static void main(String[] args) {
ReferenceCountingGc objA = new ReferenceCountingGc();
ReferenceCountingGc objB = new ReferenceCountingGc();
objA.instance = objB;
objB.instance = objA;
objA = null;
objB = null;
}
}2. Native Method Stack JNI(Java Native Interface) Referenced object
3. Object referenced by static property of method area class
4. Object referenced by method area constant pool
5. Objects referenced by active threads

Two 、 Common reference types
public static User user = new User();Soft citation : Use the object as SoftReference Object wrap of soft reference type , Normal conditions will not be recycled , however GC After that, I found that I couldn't release space to store new objects , Then the soft reference objects will be recycled . Soft references can be used to implement memory sensitive caching .
public static SoftReference<User> user = new SoftReference<User>(new User());public static WeakReference<User> user = new WeakReference<User>(new User());3、 ... and 、finalize() Methods to determine whether the object is alive or not
Four 、 How to judge a class is useless
5、 ... and 、 Garbage collection algorithm
Generational collection algorithm
According to the different life cycle of objects, different garbage collection algorithms are used . Almost all Java The heap uses this algorithm .
Copy algorithm
Divide the memory into object faces and free faces . When the memory of the object face runs out , Copy the live object in the object face to the memory of the free face , Then clear the object memory of the object face .
characteristic : No memory fragmentation , Because it uses continuous memory allocation , Make it simple and efficient ,“ Mark - eliminate ” or “ Mark - Arrangement ” The algorithm will be slower than the copy algorithm 10 More than times . Copy algorithm It is suitable for scenes with low survival rate of objects , For example, the garbage collection of the younger generation . It is necessary to use the backup memory to deal with the situation that the free side memory is insufficient .
Mark - Clear algorithm
After finding garbage objects and marking them with reachability algorithm , The heap memory will be traversed linearly from beginning to end , Reclaim unreachable objects . Then, the identifier that was originally marked as a reachable object is cleared , For the next recycling .
shortcoming : Low efficiency , Neither marking nor clearing is efficient . Easy to cause memory fragmentation , When a large object needs to allocate space , It is easy to trigger another time because you cannot find enough continuous memory GC.
Mark - Sorting algorithm
After finding garbage objects and marking them with reachability algorithm , Move all living objects , And sort by memory address , Then recycle all the memory after the end memory address . It costs more to recover , But there is no memory fragmentation . It is suitable for scenes with high object survival rate .
6、 ... and 、 Garbage collector

1.1 Serial The collector (-XX:+UseSerialGC -XX:+UseSerialOldGC)

1.2 Parallel Scavenge The collector (-XX:+UseParallelGC( The younger generation ),-XX:+UseParallelOldGC( Old age ))

1.3 ParNew The collector (-XX:+UseParNewGC)

1.4 CMS The collector (-XX:+UseConcMarkSweepGC(old))

7、 ... and 、 Garbage collection underlying algorithm implementation
Tri-colour marking

Multi label - Floating garbage
Multiple tags are only used in the garbage collection process because the worker thread is executing concurrently , A condition that causes a garbage object to be marked as a non garbage object , Floating garbage generated due to multi-target can only be used next time GC It's time to recycle . Multi tagging occurs during concurrent tagging and concurrent cleanup , There are two situations : One 、 Objects that have been scanned and marked as non garbage are released as garbage at this stage , In this case, it will be treated as a non garbage object . Two 、 New objects are generated , It will also be treated as a non garbage object .
Missing mark - Read write barrier
Missing labels will cause non garbage objects to be recycled as garbage objects , Belong to serious bug. There are two ways to do this :
Incremental updating : A black object adds a reference to a white object , Then add this reference to a collection , At the same time, black objects become gray objects . In the relabeling phase , The gray objects will be rescanned .
Original snapshot : When the reference of a gray object to a white object is deleted , This reference relationship is also stored in a collection . In the relabeling phase , The white object will be found according to the reference relationship , At the same time, change the white object to the black object , Avoid being recycled as garbage . Of course , This process may also produce floating garbage .
Incremental update and original snapshot are two ways to deal with missing targets , Generally, there is only one way in the garbage collector . They are all implemented through the write barrier . The so-called writing barrier , Before and after the assignment operation , Add some processing . For example, when adding a reference, the post write barrier record is used after the reference is completed , Use pre write barrier records before deleting references .
边栏推荐
- vscode korofileheader 的配置
- 函数栈帧的形成与释放
- Leetcode99 week race record
- JS to implement bidirectional data binding
- 资深【软件测试工程师】学习线路和必备知识点
- 【入门】正则表达式基础入门笔记
- Comprehensive application of OpenCV in contour detection and threshold processing
- Unity中跨平台获取系统音量
- Configuring the help class iconfiguration in C # NETCORE
- QListWidget中的内容不显示
猜你喜欢

cpu-z中如何查看内存的频率和内存插槽的个数?

Leetcode298 weekly race record

My opinion on test team construction

Proxy-Reflect使用详解
![Senior [Software Test Engineer] learning route and necessary knowledge points](/img/51/1be2e0812a6bca9e5e8d14bf254254.png)
Senior [Software Test Engineer] learning route and necessary knowledge points

426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary

Codeforces Round #802 (Div. 2)
![[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design](/img/80/c258817abd35887c0872a3286a821f.png)
[FPGA] realize the data output of checkerboard horizontal and vertical gray scale diagram based on bt1120 timing design

Multithreading basic part2

多线程基础部分Part2
随机推荐
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
How to check the frequency of memory and the number of memory slots in CPU-Z?
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
Spark 之 Projection
LeetCode-515. Find the maximum value in each tree row
美摄云服务方案:专为轻量化视频制作场景打造
C# netcore中 配置帮助类IConfiguration
《汇编语言-王爽》第3章笔记及实验
QListWidgetItem上附加widget
leetcode299周赛记录
【QT小点】QT下载链接
块级元素&行内元素
Nlp-d62-nlp competition d31 & question brushing D15
Spark's projection
mysql 查询时将状态改为相对应的文字
693. alternate bit binary number
JVM整体结构解析
426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
多线程基础部分Part2
力扣 179、最大数