- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
Java GC 是否通过将弱引用放置在分配给该引用的 ReferenceQueue 上以原子方式清除弱引用?换句话说,如果对 WeakReference.get() 的调用返回 null,那么 WeakReference 会在队列中吗?
最佳答案
让我们剖析 Javadoc对于弱引用:
Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.
At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references.
At the same time it will declare all of the formerly weakly-reachable objects to be finalizable.
At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.
看起来步骤是:
判断对象是否弱可达。
从 WeakReference
中清除该对象。 弱引用.get
将返回 null
。
将该对象标记为可终结的。
紧接着或在其他时间将WeakReference
放入队列(如果 WeakReference
是用一个队列)。
这意味着即使 WeakReference.get()
返回 null
也不能保证 WeakReference.enqueue
会是 true
或 ReferenceQueue.poll
不会返回 null
。
参见 https://community.oracle.com/blogs/enicholas/2006/05/04/understanding-weak-references了解更多。
Reference queues
Once a WeakReference starts returning null, the object it pointed to has become garbage and the WeakReference object is pretty much useless. This generally means that some sort of cleanup is required;WeakHashMap, for example, has to remove such defunct entries to avoid holding onto an ever-increasing number of deadWeakReferences.
The ReferenceQueue class makes it easy to keep track of dead references. If you pass a ReferenceQueueinto a weak reference's constructor, the reference object will be automatically inserted into the reference queue when the object to which it pointed becomes garbage. You can then, at some regular interval, process the ReferenceQueue and perform whatever cleanup is needed for dead references.
显示它的示例代码:
public static class A {
}
public static void main(String[] args) throws Exception{
A a = new A();
ReferenceQueue<A> rq = new ReferenceQueue<A>();
WeakReference<A> aref = new WeakReference<A>(a, rq);
a = null;
//aref.get() should be a, aref.isEnqueued() should return false, rq.poll() should return null
System.out.println( "0: " + aref + " : " + aref.get() + " : " + aref.isEnqueued() + " " + rq.poll() );
Thread.sleep(1000);
System.out.println("Running GC.");
Runtime.getRuntime().gc(); //let GC clear aref
System.out.println("GC ran.");
//aref.get() should be null, aref.isEnqueued() should return false, rq.poll() should return null
System.out.println( "1: " + aref + " : " + aref.get() + " " + aref.isEnqueued() + " " + rq.poll() );
//give some time for GC to enqueue aref
Thread.sleep(1000);
//ref.get() should be null, aref.isEnqueued() should return true, rq.poll() should return aref
System.out.println( "2: " + aref + " : " + aref.get() + " " + aref.isEnqueued() + " " + rq.poll() );
}
关于java - 弱引用通过放置在引用队列中以原子方式清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48431100/
我一直在尝试编写我自己的弱/强指针,但我并不清楚其中的关系。我似乎遇到的所有事情都没有说清楚,而且一个医生经常会反驳另一个医生所说的话。任何人都可以详细解释弱/强指针关系,也许还有图像或代码示例吗?
静态/动态和强/弱类型之间有什么区别? 最佳答案 静态/动态类型涉及何时获取类型信息(在编译时或运行时) 强/弱类型是关于如何严格区分类型(例如,语言是否尝试从字符串到数字进行隐式转换)。 请参阅wi
我有一个非常奇怪的情况。我的服务器当前已关闭并收到 503 http 状态代码。基于如下给定的代码,代码进入 if 条件,但是当我将调试点置于 let error = self?.decodeErro
对于短期运行的操作,避免[weak self]是否可以接受?例如,URLSession 将保留 dataTask(with:completion:) 的闭包: final class ViewCont
我有一个非常奇怪的情况。我的服务器当前已关闭并收到 503 http 状态代码。基于如下给定的代码,代码进入 if 条件,但是当我将调试点置于 let error = self?.decodeErro
假设我有以下情况: Test1.java import java.lang.ref.WeakReference; public class Test1 { public WeakReferen
有没有办法告诉模拟器(我正在使用 Modelsim)当信号不是由任一双向接口(interface)驱动时将信号拉到弱“H”? 例如,如果我有一个 I2C 信号 I2C_SDA 被声明为来自 2 个模块
这是将一些值放入 WeakHashMap 中然后从映射中删除这些值的代码片段。它如何处理分配的内存? import java.util.*; public class WeakHashMap_Main
我正在尝试弄清楚智能指针可以实现什么。 但有一些感觉像是障碍。 普通指针有一个简短的定义 Someclass *p但是智能指针有点长shared_ptr p当您必须处理这些指针的模板(如 vector
这两行代码有区别吗? __weak IBOutlet UITextField *usernameField; @property (weak) IBOutlet UITextField *userna
我最近发现了 WeakHashMap Java 中的数据结构。 但是,我不明白它在不再正常使用时对映射进行垃圾收集是什么意思。数据结构如何知道我将不再在我的程序中使用 key ?如果长时间不引用 ke
我的问题是为什么 weak IBOutletCollection 总是 nil?如果将弱变强,我所有的按钮都在那里,这真的很奇怪。我试图理解苹果的逻辑,我看不出单个按钮和一组按钮在内存管理方面没有区别
我创建一个 WeakHashMap 为 WeakHashMap map = new WeakHashMap(); map.put(emp,"hello"); 其中 emp 是一个 Employee 对
在delphi sydney中,在对象(不是接口(interface))前面设置[weak]会受到惩罚吗?示例: TMyObject = class(Tobject) Private
在delphi sydney中,在对象(不是接口(interface))前面设置[weak]会受到惩罚吗?示例: TMyObject = class(Tobject) Private
众所周知,我们将声明一个可以打破强引用循环的弱委托(delegate)对象: // MyObject.h ... @property (nonatomic, weak) id delegate; ..
我已阅读this article关于Java中不同类型的引用(强引用、软引用、弱引用、幻像引用),但我不太理解。 这些引用类型之间有什么区别?每种类型何时使用? 最佳答案 Java 提供了两种不同类型
我突然想到...我相信弱引用的生命 与该引用的范围(在函数内或全局内)相关。 所以我想知道,只要我将数据处理保持在特定范围内,那么我应该可以使用 weak 与 strong 引用。正确的? 我问的原因
func addAdditionalElement(_ additionalSelectedElementsIDs: [String], startX: CGFloat, containerView:
我想要一个指针,以便我可以判断引用计数何时为 1。本质上,指针的工作方式类似于 weak_ptr,但清理工作需要手动进行。也就是说,程序每隔一段时间就会经历一个指针循环,并检查哪些指针只剩下一个引用。
我是一名优秀的程序员,十分优秀!