当前位置:网站首页>What is the difference between strong reference, soft reference, weak reference and virtual reference?
What is the difference between strong reference, soft reference, weak reference and virtual reference?
2022-07-24 11:21:00 【51CTO】
Strong citation 、 Soft citation 、 Weak reference 、 Virtual reference · Language sparrow (yuque.com)
since JDK1.2 Start ,Java Provides 4 Kinds of reference relationships , To represent the relationship between the reference and the instance object .
Strong citation “ Live forever ”
Strong citation , Is our most common common common common object reference .
As long as strong quotes still exist , The garbage collector will never recycle the referenced instance object .
Soft citation “ Yes n A second chance to live ”
Before the system is about to run out of memory , The garbage collector will recycle instance objects that are only associated with soft references , If there is not enough memory for this collection , An out-of-memory exception is thrown (OutOfMemoryError).
Soft references are often used to implement memory sensitive caching :
- If you have free memory , You can keep the cache temporarily ;
- If there is not enough memory , Then clean up the cache ;
This ensures that the cache is used at the same time , Does not run out of memory .
Weak reference “ Recycle and die ”
Instance objects associated with weak references can only survive until the next garbage collection , When the garbage collector is working , Whether the current memory is enough or not , Will recycle the instance objects that are only weakly referenced .
Weak references can be used to build a relationship without specific constraints , such as , Maintain a non mandatory mapping relationship , If the object is still there when you try to get it , Just use it , Otherwise, it will be realistic and exemplary . Weak references are also the choice of many cache implementations .
Virtual reference “ It can be recycled at any time ”
Virtual references are also called “ Ghost quotes ” perhaps “ Phantom reference ”, Virtual reference is the weakest kind of reference relationship .
- stay Java 8 And in previous versions , After virtual reference recycling , The object pointed to by the virtual reference will be recycled .
- stay Java 9 And the latest version , Virtual references have no effect on the lifetime of objects .
Cannot get an instance object by virtual reference .
Virtual references are mainly used to track the garbage collection activities of objects , Reclaim resources associated with objects .
stay Java 8 And in previous versions , When the garbage collector is ready to recycle an instance object , If it's found to have virtual references , Before recycling the instance object , Add this virtual reference to the reference queue associated with it . If the program finds that a virtual reference has been added to the reference queue , Then you can take the necessary action before the memory of the referenced object is recycled , Then disconnect the reference of the virtual reference object , Virtual references are recycled or unreachable , The object pointed to by the virtual reference will be recycled .
Reference material
ThreadLocal Principle analysis and memory leak demonstration
边栏推荐
- 07【Path、Files类的使用】
- Docker installs 3 master and 3 slave redis clusters
- Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
- 【C】 Recursive and non recursive writing of binary tree traversal
- [golang] golang implements the post request to send form type data function
- [attack and defense world web] difficulty five-star 15 point advanced question: ics-07
- Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
- [golang] golang实现截取字符串函数SubStr
- Publish local image to private library
- 周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
猜你喜欢

STM32+ESP8266+MQTT协议连接阿里云物联网平台

Lanqiao cup provincial training camp - stack and recursion

LDR6028充电OTG直播线直播声卡音频转接器最具性价比方案

Introduction to kubernetes Basics

View the source code of idea Download

Conversion between hexadecimal string and byte array

Performance test summary (I) -- basic theory

Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution

黑马瑞吉外卖之员工信息分页查询

《Nature》论文插图复刻第3期—面积图(Part2-100)
随机推荐
RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
"Low power Bluetooth module" master-slave integrated Bluetooth sniffer - help smart door lock
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
Yum installation prompt to protect multi library version
How to go from functional testing to automated testing?
【Golang】golang实现md5加密函数
[golang] golang implements the string interception function substr
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
About [software testing - interview skills and precautions for automated testing] - talk freely
MySQL queries tables and fields according to comments
What is the difference between low code and no code?
MySQL查询字段匹配某个规则的记录
Simply use MySQL index
Detailed explanation and example demonstration of Modbus RTU communication protocol
Blue Bridge Cup provincial match training camp - Calculation of date
Reprint: getting started with cache coherence
Linux redis download and installation
自动推理的逻辑06--谓词演算
Four components and working principle of frequency converter
【Golang】golang中map元素的删除和清空