开发者

Definition: Unfinalized versus finalizable object

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开发者_如何学JAVA 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


The answer seems to lie in this line:

If the Java virtual machine detects that an unfinalized object has become finalizer-reachable or unreachable, it may label the object finalizable (G, H);

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.

  • a unfinalized object will not get its finalizer automatically invoked by the JVM in this state
  • a finalizable object can eventually get its finalizer automatically invoked by the JVM


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜