当前位置:网站首页>Garbage collector and memory allocation strategy
Garbage collector and memory allocation strategy
2022-06-25 17:44:00 【[email protected]】
List of articles
1. Reference counting algorithm
Add the counter that should be referenced to the object , Whenever there's a place to quote it , The counter value adds 1; When the reference fails, the counter value is decremented by one ; When the counter is 0 It is impossible to be used again
- The problem is : It can't solve the problem of circular reference between objects
objA,instance=objB;
objB.instance=objA;
objA=null;
objB=null;
2. Reachability analysis algorithm
adopt GC Roots As the starting point , Start with these nodes and drill down , The search path is called the reference chain , When an object arrives GC Roots There's no chain of references connected , Then prove that this object is not available

It can be used as GC Roots The object of :
- Objects referenced in the virtual machine stack
- Class static attribute references in the method area
- The object referenced by a constant in the method area
- Objects referenced in the local method stack
3. Quote categories
- Strong citation :
Object obj=new Object(), As long as strong quotes still exist ,GC Objects are not recycled - Soft citation : Describe some useful but not necessary objects , When the memory is insufficient, the object pointed to by the soft reference is recycled
- Weak reference : Describe nonessential objects , The next time GC At the beginning, whether the memory is sufficient or not , It's all recycled
- Virtual reference : The weakest kind of reference relationship , Whether an object has a virtual reference , It doesn't affect their survival time at all
4.GC Roots Is the object bound to die when unreachable
- not always
- GC Mark for the first time and filter for the first time when it is not reachable
- If the object is not overridden finalize Methods or finalize The method has already been executed , The object has no chance of survival
- If the object overrides finalize Method and finalize The party has not implemented , stay fianlize Method to re-establish the relationship with the objects on the reference chain , The object will not be recycled
- Of an object fianlize Method will only be executed once
package test.JVM;
public class FinalizeEscapeGC {
public static FinalizeEscapeGC SAVE_HOOK=null;
public void isAlive()
{
System.out.println("yes,i am still alive");
}
@Override
protected void finalize() throws Throwable
{
super.finalize();
System.out.println("finalize method executed");
FinalizeEscapeGC.SAVE_HOOK=this;
}
public static void main(String[] args) throws InterruptedException {
SAVE_HOOK=new FinalizeEscapeGC();
SAVE_HOOK=null;
System.gc();
Thread.sleep(500);
if(SAVE_HOOK!=null)
{
SAVE_HOOK.isAlive();
}
else
{
System.out.println("no,i am dead");
}
SAVE_HOOK=null;
System.gc();
Thread.sleep(500);
if(SAVE_HOOK!=null)
{
SAVE_HOOK.isAlive();
}
else
{
System.out.println("no,i ma dead");
}
}
}
finalize method executed
yes,i am still alive
no,i ma dead
5. Recovery method area
- The garbage collection of the permanent generation is divided into two parts : Discard constants and useless classes
- Determine whether a constant is an obsolete constant :
- No object references constants in the constant pool
- Determine whether a class is useless :
- All instances of this class have been recycled
- Load the class ClassLoader Has been recovered
- Corresponding to this class java.lang.Class Object has no references
6. Garbage collection algorithm
6.1 Tag clearing algorithm
- Mark all the objects that need to be recycled first
- After marking, all marked objects will be recycled
- Low efficiency
- A large number of discontinuous memory fragments will be generated , As a result, there is not enough memory to allocate large objects later

6.2 Copy algorithm
- Divide the available memory into two equal sized blocks according to capacity , Use only one piece at a time
- A block of memory is used up , Just copy the living object to another piece , Then clean up the used memory space at one time
- Memory costs are high , waste 50% Of memory space

6.3 Mark - Sorting algorithm
- For the old age
- Marking process and “ Mark - eliminate ” Algorithm is the same , However, objects are not directly recycled later , Instead, let the living object move to one side , Then directly clean up the memory outside the end boundary

6.4 Generational collection algorithm
- The memory is divided into several blocks according to the life cycle of the object
- Java The heap is divided into The new generation and Old age
- The new generation : A lot of people die , Little survival , Copy algorithm
- Old age : The survival rate is high , There is no additional space to guarantee its distribution , Mark - Clean up algorithms or tags - Sorting algorithm
7. Enumerated root node
- The sensitivity of reachability analysis to execution time is reflected in GC Pause on , During reachability analysis, the whole execution system looks as if it is frozen at a certain point in time
- GC There must be a pause in the progress of all Java Execute thread
8. safer
- The program doesn't stop and start everywhere GC, Only when the safety point is reached can it pause
- Safety points can't be too few , Too little will lead to GC The wait is too long
- Don't be too safe , Too much will increase the load at run time
- The thread is in GC There are two ways to interrupt : Preemptive interrupt and Active interrupt
- Preemptive interrupt :GC when , First, interrupt all threads , If the place where the thread is interrupted is not found at the safe point , Just restore the thread , Let the thread run to the safe point , But few virtual machines do this
- Active interrupt : Set a flag , This flag is actively rotated during thread execution , When the interrupt flag bit is found to be true, it suspends its own interrupt
9. The safety area
- For being in Sleep perhaps Blocked Thread in state , It's unresponsive JVM Interrupt request for , You can't go to a safe place to suspend , A safe area is required
- A security zone is defined as a code segment , The quoting relationship doesn't change , Anywhere in the area is GC Safe , It can be seen that the safety zone is an expanded safety point
10. Memory allocation and recycling strategy
10.1 Priority is given to Eden Distribute
- Most of the time , The object is the new generation Eden Regional distribution , When Eden There is not enough space to allocate , The virtual machine will do it once GC
10.2 Large objects go directly into the old generation
- Large objects are those that require a large amount of continuous memory space Java object
10.3 Long term survivors will enter the old age
- The virtual machine sets an object age counter for each object , If the object is Eden Born and passed for the first time Minor GC Still alive after , Then set the age of the object to 1
- The age of the subject increases to 15 Will be promoted to the old generation
10.4 Dynamic object age determination
- The virtual machine does not necessarily judge whether the object has entered the old generation according to the age counter
- If in Survivor The sum of all object sizes of the same age in the space is greater than Survivor Half of the space , Those who are older than or equal to this age can directly enter the elderly generation
10.5 Space allocation guarantee
- In the event of a Minor GC Before , Virtual opportunity first checks whether the largest available continuous space of the old generation is larger than the total space of all objects of the new generation , Greater than is safe , Otherwise it's not safe
- Virtual opportunity viewing when not secure HandlePromotionFailure Whether the set value allows the guarantee to fail
- If allowed , Continue to check whether the maximum available continuous space of the elderly generation is larger than the average size of objects promoted to the old age
- If it is greater than , Conduct Minor GC
- If less than or not allowed , Conduct Full GC
Bibliography :《 Deepen understanding Java virtual machine :JVM Advanced features and best practices ( The first 2 edition )》
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190532337911.html
边栏推荐
- Vscode / * * generate function comments
- How about qiniu's Zhangle TenPay? Is it safe
- [matlab] data statistical analysis
- 杰理之增加加密文件播放功能【篇】
- Use diskgenius to expand the capacity of system disk C
- How high does UART baud rate require for clock accuracy?
- 使用DiskGenius拓展系统盘C盘的容量
- CVPR小目标检测:上下文和注意力机制提升小目标检测(附论文下载)
- 启牛的涨乐财付通如何?安全靠谱吗
- Mathematical modeling - linear programming
猜你喜欢

杰理之如何给外界输出一个时钟源使用【篇】

UART波特率对时钟精度的要求有多高?

使用DiskGenius拓展系统盘C盘的容量

Intelligent dialog 01 redis installation

How high does UART baud rate require for clock accuracy?

Mathematical modeling - linear programming

【UVM实战 ===> Episode_1 】~ MCDF设计更新、AMBA标准接口、UVM验证环境更新

BILSTM和CRF的那些事
![[compilation principle] lexical analysis](/img/b2/8f7dea3944839e27199b28d903d9f0.png)
[compilation principle] lexical analysis
沁恒CH583 USB 自定义HID调试记录
随机推荐
什么是算子?
Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry
Introduction to the container of() function
mysql mysql-8.0.19-winx64 安装与navicat连接
杰理之SPI 从机使用注意事项【篇】
Bilstm and CRF
Golang sort slice int
杰理之如何给外界输出一个时钟源使用【篇】
杰理之获取复位源和唤醒的 IO 口的方法【篇】
有关QueryInterface函数
MySQL mysql-8.0.19-winx64 installation and Navicat connection
Distributed remote management of distribution room environment
数据挖掘之时间序列分析[通俗易懂]
Can I open an account? Is it safe to open an account
Mobx learning path - powerful "state management tool"
Agent white paper - jointly build agents and create the wisdom of the whole scene | cloud library No.21 recommendation
启牛涨乐财付通下载是可以开户吗?开户安全吗
使用DiskGenius拓展系统盘C盘的容量
【编译原理】词法分析
bert之我的小总结