当前位置:网站首页>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
边栏推荐
- Hyper-v:在虚拟网络中启用 SR-IOV
- Shortcut keys for the rainbow brackets plug-in
- [Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**
- TFTP download kernel, NFS mount file system
- Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.
- What did Tongji and Ali study in the CVPR 2022 best student thesis award? This is an interpretation of yizuo
- 5g has been in business for three years. Where will innovation go in the future?
- Combination of applet container and Internet of things
- China Infrastructure Development Association: electronic contract is recommended
- 如何写一个技术方案
猜你喜欢

News management system based on SSM

K-line diagram must be read for quick start

Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence

Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides

What will be the game changes brought about by the meta universe?

Course design for the end of the semester: product sales management system based on SSM

ABAP-发布Restful服务

编译生成busybox文件系统

Building a basic buildreoot file system

港科大&MSRA新研究:关于图像到图像转换,Finetuning is all you need
随机推荐
Compile and generate busybox file system
基于eNSP的校园网设计的仿真模拟
流批一体在京东的探索与实践
Ten thousand volumes - list sorting [01]
[Netease Yunxin] playback demo build: unable to convert parameter 1 from "asyncmodalrunner *" to "std:: nullptr\u T"**
Taishan Office Technology Lecture: how to align and draw words of different sizes on the same line
Radio and television 5g officially set sail, attracting attention on how to apply the golden band
Plane intersection and plane equation
Redis (IV) - delete policy
Post MSF infiltration summary
Shortcut keys for the rainbow brackets plug-in
TFTP下载kernel,nfs挂载文件系统
Zero foundation can also be an apple blockbuster! This free tool can help you render, make special effects and show silky slides
6 张图带你搞懂 TCP 为什么是三次握手?
Key to understanding the trend of spot Silver
同济、阿里的CVPR 2022最佳学生论文奖研究了什么?这是一作的解读
Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
[machine learning] K-means clustering analysis
Nft: unlimited possibilities to open the era of encryption Art
Generate confrontation network, from dcgan to stylegan, pixel2pixel, face generation and image translation.