java - 为什么幻影引用在排队时没有被清除?

标签 java garbage-collection

我们可以看到“幻影可达”和“不可达”一样不可达:§

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.

现在,来自: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.

基本原理是什么?有没有?

这是 Java API 怪癖的又一个典型案例吗?

最佳答案

软引用在入队时会被清除,因为软引用的主要用途是允许缓存大对象,而清除软引用允许对缓存的大对象进行垃圾回收。

弱引用在入队时被清除,因为弱引用的主要用途是允许一个人引用一个对象而不阻止它被垃圾收集,所以一旦对象入队就清除引用允许对象被垃圾收集.

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

这解释了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.

关于java - 为什么幻影引用在排队时没有被清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7048767/

相关文章:

java - 在这种情况下,什么时候会对我的类实例调用 Finalize() ?

java - iText 生成的 PDF -> 将 PDF 保存到本地驱动器后文本丢失

java - 隐式变量转换

c# - 是否可以编写 C# 以便在对象超出范围时对其进行垃圾回收?

java - G1 : What are the differences between mixed gc and full gc?

C# 垃圾收集事件根

Java:绑定(bind)和连接方法

java - 检查方法是否需要使用注释的参数并反射(reflect)

java - 消息线程安全

java GC : what programming style makes safe point attainment easier (faster)?