为什么幽灵引用没有被清除,因为它们入队了? [英] Why are Phantom References not cleared as they are enqueued?

查看:103
本文介绍了为什么幽灵引用没有被清除,因为它们入队了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以看到phantom reachable与unreachable一样无法访问:§


一个对象是幻像可达如果它既不强烈,也不柔弱,也不可达,它已经完成,并且一些幻影引用指向它。

最后,一个对象是 >无法访问,因此无法通过上述任何方式获得回收。


现在,来自: http://download.oracle。 com / javase / 6 / docs / api / java / lang / ref / PhantomReference.html b
$ b


幻像引用不会由垃圾收集器自动清除,因为它们是enque UED。通过幻影引用可以访问的对象将保持如此状态,直到所有这些引用被清除或者自己变得无法访问。

是否还有一个?



这是Java API quirk的另一个典型情况吗?

/ div>当引用时,软引用会被清除,因为主引用允许缓存大对象,并且清除软引用允许大型缓存对象被垃圾回收。



弱引用在排入队列时被清除,因为弱引用的主要用途是允许引用一个对象而不会阻止它被垃圾回收,因此只要对象排入队列就清除引用,这样就可以使对象成为垃圾收集。



在排队时,幻影引用不会被清除,因为一个幽灵引用的用例允许在之前执行清理 。通过不清除引用,对象保持幻影可达(并且不符合收集垃圾的条件),直到用户清除PhantomReference对象或PhantomReference自身收集垃圾。



这是解释这里


如果一个对象既不强烈,也不柔弱或不可达,它已经完成,并且一些幻影引用指向它。

最后,一个对象是无法访问的,因此当以上述任何一种方式无法访问时,都有资格进行回收。

/ p>


We can see that "phantom reachable" is as unreachable as "unreachable": §

An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.

Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.

Now, from: http://download.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html

Unlike soft and weak references, phantom references are not automatically cleared by the garbage collector as they are enqueued. An object that is reachable via phantom references will remain so until all such references are cleared or themselves become unreachable.

What's the underlying rationale? Is there even one?

Is this yet another typical case of Java API quirk?

解决方案

Soft references are cleared when enqueued because the primary use soft references are to allow caching large objects, and clearing the soft references allows the large cached object to be garbage collected.

Weak references are cleared when enqueued because the primary use of weak references are to allow one to reference an object without preventing it from being garbage collected, so clearing the references as soon as the object is enqueued allows the object to be garbage collected.

Phantom references are not cleared when enqueued since one use case of phantom references is to allow performing cleanup before an object is garbage collected. By not clearing the references, the object remains phantomly reachable (and not eligible for garbage collected) until after the PhantomReference to that object is cleared by the user, or the PhantomReference is itself garbage collected.

This is explained here,

An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.

Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.

这篇关于为什么幽灵引用没有被清除,因为它们入队了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆