当前位置:网站首页>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 .
边栏推荐
- LeetCode-515. Find the maximum value in each tree row
- Codeforces Round #802 (Div. 2)
- Add widget on qlistwidgetitem
- 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
- Unity中跨平臺獲取系統音量
- Spark 之 Projection
- 汇编语言-王爽 第8章 数据处理的两个基本问题-笔记
- 427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
- C语言练手小项目(巩固加深知识点理解)
- 310. 最小高度树
猜你喜欢

Implementation of easyexcel's function of merging cells with the same content and dynamic title
![Senior [Software Test Engineer] learning route and necessary knowledge points](/img/51/1be2e0812a6bca9e5e8d14bf254254.png)
Senior [Software Test Engineer] learning route and necessary knowledge points

leetcode299周赛记录

汇编语言-王爽 第13章 int指令-笔记

Run opcua protocol demo on raspberry pie 4B to access kubeedge

信息系统项目管理师---第七章 项目成本管理

IAR Systems全面支持芯驰科技9系列芯片

资深【软件测试工程师】学习线路和必备知识点

How win 10 opens the environment variables window

Leetcode298 weekly race record
随机推荐
Formation and release of function stack frame
[collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
NEON优化1:软件性能优化、降功耗怎么搞?
JVM调优思路
【Cocos Creator 3.5.1】this.node.getPosition(this._curPos)的使用
IP网络通信的单播、组播和广播
思维的技术:如何破解工作生活中的两难冲突?
How to check the frequency of memory and the number of memory slots in CPU-Z?
使用 WordPress快速个人建站指南
What's new in redis4.0 - active memory defragmentation
js实现双向数据绑定
[FPGA] design and implementation of frequency division and doubling based on FPGA
免费的 SSH 和 Telnet 客户端PuTTY
数据库-索引
Unity中跨平臺獲取系統音量
【QT小记】QT中正则表达式QRegularExpression的基本使用
Qt使用Valgrind分析内存泄漏
汇编语言-王爽 第11章 标志寄存器-笔记
Spark 之 WholeStageCodegen
Code is data