定义:未最终化对象与可最终化对象
为了理解 Java 中的弱引用,我不得不查阅 Java 语言规范。第 12.6 节中的以下部分让我感到困惑:
未终结的对象从未自动调用其终结器; 已完成的对象已自动调用其终结器。一个可最终确定的 对象从未自动调用其终结器,但 Java 虚拟对象 机器最终可能会自动调用其终结器。
那么未最终化对象和可最终化对象之间的形式区别是什么?从引用看来,如果未最终化和可最终化是不同的,那么对于未最终化的对象来说,JVM 最终可能会调用其终结器,这一定是不。有点令人困惑,或者我还有一些英语语义需要学习;)
链接到 Java 规范中的部分:实现终结
In order to understand weak references in Java, I have had to consult the Java Language Specification. The following part, from section 12.6, puzzles me:
An unfinalized object has never had its finalizer automatically invoked;
a finalized object has had its finalizer automatically invoked. A finalizable
object has never had its finalizer automatically invoked, but the Java virtual
machine may eventually automatically invoke its finalizer.
So what is the formal difference between an unfinalized and a finalizable object ? From the quote it seems that if unfinalized and finalizable are to be different, then for an unfinalized object it must be the case that it is not true that the JVM may eventually invoke its finalizer. A little confusing or I still have some English semantics to study ;)
Link to the section in the Java spec: Implementing Finalization
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案似乎就在这一行:
未最终确定的对象尚不符合最终确定的条件。他们是可以到达的。可终结的对象有资格被终结,因此 JVM 可以在选择时执行此操作。换句话说,“可能”的含义是“有权”,而不仅仅是“可能发生”的含义。
The answer seems to lie in this line:
Unfinalized objects are not eligible for finalization yet. They are reachable. Finalizable objects are eligible to be finalized, so the JVM may do that when it chooses. In other words, "may" in the sense of "has permission to" not just in the sense of "it might happen."
未终结对象和可终结对象之间的区别在于,第二个对象上的终结器可以在将来的任何时间自动调用,而未终结对象上的终结器不能除非该对象首先变为可终结的,否则将自动调用。
The difference between an unfinalized and a finalizable object is that the finalizer on the second one could be automatically invoked at any time in the future, while the finalizer on the unfinalized object can't be automatically invoked, unless the object first becomes finalizable.
不保证 GC 会被执行,也不保证
finalize()
会被调用。它很可能会在某个时候发生。当一个对象不再有强引用时,它可以被垃圾收集。一段时间后,可以执行 GC,并将对象添加到终结队列中,以调用其
finalize()
方法。一旦该方法被调用,如果仍然没有对它的强引用,则可以将其删除。There is no guarantee that a GC will ever be performed or that
finalize()
will ever be called. It is highly likely that it will happen at some point.When an object no longer has a strong reference to it, it can be garbage collected. Some time later a GC can be performed and the object is added to a finalization queue to have its
finalize()
method called. Once the method has been called it can be removed if there still is not strong reference to it.