当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
30个单片机常见问题及解决办法!
WebRTC系列-网络传输之7-ICE补充之提名(nomination)与ICE_Model
Navigation [machine learning]
The SCP command is used in the expect script. The perfect solution to the problem that the SCP command in the expect script cannot obtain the value
G1和ZGC垃圾收集器
Jump details of item -h5 list, and realize the function of not refreshing when backing up, and refreshing when modifying data (record scroll bar)
信息系统项目管理师---第七章 项目成本管理
Spark 之 built-in functions
【合辑】点云基础知识及点云催化剂软件功能介绍
Assembly language - Wang Shuang Chapter 3 notes and experiments
MATLAB快速将影像的二维坐标转换为经纬度坐标
Nlp-d62-nlp competition d31 & question brushing D15
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
使用CSDN 开发云搭建导航网站
微信小程序WebSocket使用案例
vscode korofileheader 的配置
Free SSH and telnet client putty
Go日志-Uber开源库zap使用
Unicast, multicast and broadcast of IP network communication
【QT小作】使用结构体数据生成读写配置文件代码









