gpt4 book ai didi

java - 弱引用通过放置在引用队列中以原子方式清除?

转载 作者:搜寻专家 更新时间:2023-10-31 20:08:40 26 4
gpt4 key购买 nike

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.

看起来步骤是:

  1. 判断对象是否弱可达。

  2. WeakReference 中清除该对象。 弱引用.get将返回 null

  3. 将该对象标记为可终结的。

  4. 紧接着或在其他时间WeakReference 放入队列(如果 WeakReference 是用一个队列)。

这意味着即使 WeakReference.get() 返回 null 也不能保证 WeakReference.enqueue 会是 trueReferenceQueue.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com