weakReference vs softReference

本文详细介绍了弱引用的概念及其在Java中的应用。弱引用允许被引用的对象在不再被强引用时被垃圾回收器回收,适用于实现自动缓存清理等功能。文中还对比了软引用、弱引用及幽灵引用的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

weak reference
A weak reference is one that does not prevent the referenced object from being garbage collected. You might use them to manage a HashMap to look up a cache of objects. A weak reference is a reference that does not keep the object it refers to alive. A weak reference is not counted as a reference in garbage collection. If the object is not referred to elsewhere as well, it will be garbage collected.

The GC will send some sort of "finalize" message to the object and then set any weakly-referencing variables to null whenever it disposes of the referenced object. This allows "finalization" logic to be run before the object is disposed of (e.g., close a file if still open, commit any open transaction(s), etc.). Java 1.1 does not support weak references other than via an undocumented Ref class that is not supported under Netscape. Weak references arrived officially with JDK 1.2. Java has three kinds of weak references, called soft references, weak references, and phantom references, in order of increasing weakness.

Java has four orders of strength in holding onto Objects. In descending order from strongest to weakest they are:

1. The JVM holds onto regular Objects until they are no longer reachable by either clients or any container. In other words Objects are garbage collected when there are no more live references to them. Dead references don’t count.
garbage collection
2. Soft references can be deleted from a container if the clients are no longer referencing them and memory is tight.
3. Weak references are automatically deleted from a container as soon clients stop referencing them.
4. Phantom references point to objects that are already dead and have been finalised.

[table]
|Soft vs Weak vs Phantom References|
|Type | Purpose |Use | When GCed |Implementing Class|
|Strong Reference | An ordinary reference. Keeps objects alive as long as they are referenced.| normal reference. |Any object not pointed to can be reclaimed. | default|
|Soft Reference | Keeps objects alive provided there’s enough memory. | to keep objects alive even after clients have removed their references (memory-sensitive caches), in case clients start asking for them again by key. | After a first gc pass, the JVM decides it still needs to reclaim more space. | java.lang.ref.SoftReference|
|Weak Reference | Keeps objects alive only while they’re in use (reachable) by clients. | Containers that automatically delete objects no longer in use. |After gc determines the object is only weakly reachable | java.lang.ref.WeakReference
java.util.WeakHashMap|
|Phantom Reference |Lets you clean up after finalization but before the space is reclaimed (replaces or augments the use of finalize()) | Special clean up processing |After finalization. | java.lang.ref.PhantomReference|

[/table]
[url]http://mindprod.com/jgloss/weak.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值