当前位置:网站首页>Deep understanding of JVM (IV) - garbage collection (I)
Deep understanding of JVM (IV) - garbage collection (I)
2022-06-30 18:03:00 【Zhaoliwen is a pig】
In depth understanding of JVM( Four )—— Garbage collection ( One )
List of articles
1、 How to judge whether an object can be recycled
Reference counting
disadvantages : When quoting circularly , The count of both objects is 1, As a result, both objects cannot be released

Reachability analysis algorithm
- JVM The garbage collector in passes through Accessibility analysis To explore all living objects
- Scan objects in the heap , See if you can follow GC Root Object as the starting point of the reference chain to find the object , If Can't find , It means that... Can be recycled
- It can be used as GC Root The object of
- Virtual machine stack ( Local variables in stack frames ) Object referenced in .
- Object referenced by a class static property in a method area
- The object referenced by a constant in the method area
- Local method stack JNI( In other words Native Method ) Referenced object
Five quotations

Strong citation
Only GC Root Don't quote When the object , To recycle Strong citation object
- Pictured above B、C Objects do not reference A1 Object time ,A1 The object will be recycled
Soft citation
When GC Root When pointing to a soft reference object , stay When there is not enough memory , Meeting Reclaim the objects referenced by soft references
- As shown in the figure above, if B Object no longer references A2 Object and out of memory , Soft reference refers to A2 The object will be recycled
Use of soft references
public class Demo1 {
public static void main(String[] args) {
final int _4M = 4*1024*1024;
// Use soft reference objects list and SoftReference Is a strong quote , and SoftReference and byte Arrays are soft references
List<SoftReference<byte[]>> list = new ArrayList<>();
SoftReference<byte[]> ref= new SoftReference<>(new byte[_4M]);
}
}
If insufficient memory is found during garbage collection , When reclaiming the object pointed to by the soft reference , Soft references themselves are not cleaned up
If you want to Clean up soft references , Need to make Use reference queue
public class Demo1 {
public static void main(String[] args) {
final int _4M = 4*1024*1024;
// Use reference queue , Used to remove soft reference objects with empty references
ReferenceQueue<byte[]> queue = new ReferenceQueue<>();
// Use soft reference objects list and SoftReference Is a strong quote , and SoftReference and byte Arrays are soft references
List<SoftReference<byte[]>> list = new ArrayList<>();
SoftReference<byte[]> ref= new SoftReference<>(new byte[_4M]);
// Traversing the reference queue , If there are elements , The remove
Reference<? extends byte[]> poll = queue.poll();
while(poll != null) {
// The reference queue is not empty , Then remove the element from the collection
list.remove(poll);
// Move to the next element in the reference queue
poll = queue.poll();
}
}
}
** The general idea is :** Check whether there are soft references in the reference queue , If there is , Remove the soft reference from the collection where it is stored ( Here is a list aggregate )
Weak reference
Only weak references refer to this object , During garbage collection , Whether the memory is sufficient or not , Will recycle the objects referenced by weak references
- As shown in the figure above, if B Object no longer references A3 object , be A3 Objects will be recycled
The use of weak references is similar to that of soft references , Just to SoftReference In exchange for WeakReference
Virtual reference
When the object referenced by the virtual reference object is recycled , The virtual reference object will be put into the reference queue , Call the method of virtual reference
- An embodiment of virtual reference is Free the memory allocated by direct memory , When the referenced object ByteBuffer After being recycled , Virtual reference object Cleaner Will be placed in the reference queue , And then call Cleaner Of clean Method to free direct memory
- Pictured above ,B Object no longer references ByteBuffer object ,ByteBuffer It will be recycled . However, the memory in direct memory has not been reclaimed . In this case, the virtual reference object Cleaner Put in the reference queue , And then call it. clean Method to free direct memory
Terminator references
All classes inherit from Object class ,Object Class has one. finalize Method . When an object is no longer referenced by other objects , The finalizer reference object will be put into the reference queue first , Then find the object it refers to according to the finalizer reference object , Then the object is called. finalize Method . After calling , The object can be garbage collected
- Pictured above ,B Object no longer references A4 object . This is the finalizer object, which will be put into the reference queue , The reference queue will be based on it , Find the object it references . And then invoke the referenced object finalize Method . After calling , The object can be garbage collected
Reference queue
- Soft quotes and weak quotes Can cooperate with Reference queue
- stay Weak reference and Virtual reference After the referenced object is recycled , These references are placed in the reference queue , It is convenient to recycle these soft / Weak references
- Virtual references and finalizer references Must cooperate Reference queue
- Virtual references and finalizer references are associated with a reference queue
边栏推荐
- 【二叉树】前序遍历构造二叉搜索树
- Advanced Mathematics (Seventh Edition) Tongji University General exercises one person solution
- Simulation of campus network design based on ENSP
- [sword finger offer] 52 The first common node of two linked lists
- 分享 5 大常用的特征选择方法,机器学习入门必看!!!
- What should I pay attention to when playing futures? Where is safe to open an account? It's my first contact
- 基于SSH的客户关系CRM管理系统
- Login box tricks
- Ardunio esp32 DH11 real time uploading temperature and humidity Alibaba cloud self built mqtt
- 【义修换届大礼包】
猜你喜欢

Canvas mouse control gravity JS effect

Hyper-V: enable SR-IOV in virtual network

基于SSH的通讯网络电子计费系统

基于SSH的客户关系CRM管理系统

编写C语言的最简单小程序Hello world

What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo

Booking UI effect implemented by svg

splitting. JS password display hidden JS effect

6 张图带你搞懂 TCP 为什么是三次握手?

4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
随机推荐
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
5g business is officially commercial. What are the opportunities for radio and television?
splitting. JS password display hidden JS effect
Ardunio esp32 DH11 real time uploading temperature and humidity Alibaba cloud self built mqtt
Exch:exchange server 2013 is about to end support
Acwing game 57
【机器学习】K-means聚类分析
The gates of Europe
大文件处理(上传,下载)思考
Several points in MySQL that are easy to ignore and forget
[Architecture] 1366- how to draw an excellent architecture diagram
What will be the game changes brought about by the meta universe?
Exch:完整性检查 Database Integrity Checking
JS from prototype chain to inheritance
MSF后渗透总结
Vue3 reactive database
Small tools (3) integration knife4j3.0.3 interface document
TFTP下载kernel,nfs挂载文件系统
Solution: STM32 failed to parse data using cjson
Shortcut keys for the rainbow brackets plug-in