What does it mean for an object to be "finalizer-reachable" in Java?

124 views Asked by At

I am reading the Java 8 spec and I see this definition for finalizer-reachable:

A finalizer-reachable object can be reached from some finalizable object through some chain of references, but not from any live thread.

What would this look like in code? I don't have an intuition for what something like this would even look like.

2

There are 2 answers

0
Stephen C On BEST ANSWER

In the example code below, when an instance of Example becomes unreachable, the object that os refers to will be finalizer-reachable.

  public class Example {

      private OutputStream os;

      public Example(OutputStream os) {
          this.os = 0s;
      }

      protected void finalize() {
          try {
              os.close();
          } catch (IOException ex) {
              // ignore it
          }
     }
  }

However, if the Example instance was no longer eligible for finalization (e.g. because it had been finalized previously and then "resurrected" during finalization), then os would not be finalizer-reachable.

The "finalizer-reachable" state is about specifying that objects that may be referred to during finalization don't get deleted prematurely. The specification does not state how this should be ensured. I imagine that it would not be possible for Java code (or even native code) to determine whether a specific object was is this state.

0
Gabe Sechan On

You have an object Foo. Foo is finalizable, and has no references in any variable in the program anymore. Foo has a member Bar. Bar references an object that is not referenced anywhere else. The object Bar references is therefor finalizer-reachable.

Basically its an object that cannot be reached through the code anymore, but can be reached through another object that cannot be reached through the code.