当前位置:网站首页>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 .
边栏推荐
- Spark 之 WholeStageCodegen
- 【Cocos Creator 3.5.1】this. node. Use of getposition (this.\u curpos)
- Using domain name forwarding mqtt protocol, pit avoidance Guide
- Get system volume across platforms in unity
- 下载cuda和cudnn
- Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
- Neon optimization 1: how to optimize software performance and reduce power consumption?
- JVM整体结构解析
- G1和ZGC垃圾收集器
- LeetCode 0086.分隔链表
猜你喜欢
随机推荐
我对于测试团队建设的意见
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
Webrtc Series - Network Transport 7 - ice Supplement nominations and ice Modèle
Database - index
Asp.Net Core6 WebSocket 简单案例
LeetCode 0086.分隔链表
QListWidget中的内容不显示
Spark 之 WholeStageCodegen
JVM类加载机制
MATLAB快速将影像的二维坐标转换为经纬度坐标
LeetCode-515. Find the maximum value in each tree row
The form verifies the variables bound to the V-model, and the solution to invalid verification
JS to implement bidirectional data binding
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
【Cocos Creator 3.5.1】event.getButton()的使用
【入门】正则表达式基础入门笔记
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
The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi
Luogu p2939 [usaco09feb]revamping trails G
How win 10 opens the environment variables window








