当前位置:网站首页>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
边栏推荐
- 水平视觉错误效果js特效代码
- Post MSF infiltration summary
- [zero basic IOT pwn] environment construction
- Animesr: learnable degradation operator and new real world animation VSR dataset
- Network: principle and practice of server network card group technology
- Plane intersection and plane equation
- DeFi借贷协议机制对比:Euler、Compound、Aave和Rari Capital
- [sword finger offer] sword finger offer 53 - ii Missing numbers from 0 to n-1
- 后渗透之文件系统+上传下载文件
- [sword finger offer] 53 - I. find the number I in the sorted array
猜你喜欢

Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!

Flutter custom component

Redis (III) - transaction

Small Tools(3) 集成Knife4j3.0.3接口文档
![[zero basic IOT pwn] environment construction](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[zero basic IOT pwn] environment construction

Exploration and practice of "flow batch integration" in JD

Booking UI effect implemented by svg

新技能:通过代码缓存加速 Node.js 的启动

Post penetration file system + uploading and downloading files

How can you choose to work in the county after graduation?
随机推荐
How can you choose to work in the county after graduation?
DeFi借贷协议机制对比:Euler、Compound、Aave和Rari Capital
元宇宙带来的游戏变革会是怎样的?
Redis (I) - data type
后渗透之文件系统+上传下载文件
6 张图带你搞懂 TCP 为什么是三次握手?
Horizontal visual error effect JS special effect code
Vue3 reactive database
Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides
【网易云信】播放demo构建:无法将参数 1 从“AsyncModalRunner *”转换为“std::nullptr_t”**
Spin lock exploration
Map集合
基于SSH的客户关系CRM管理系统
canvas云朵形状动画
Importing alicloud ECS locally to solve deployment problems
MySQL之零碎知识点
. Net ORM framework hisql practice - Chapter 1 - integrating hisql
现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
Building a basic buildreoot file system
阿里云ECS导入本地,解决部署的问题