Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "traversing" a WeakReference require invocation of get()?

Tags:

java

I'm interested in the WeakReference specification, not so much how to use it.

The specification for the term "weakly reachable" says, in part:

An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference.

I am interested in what "traversing" means.

The obvious conclusion is: to traverse a WeakReference, you call its get() method, yielding the referent. I understand that.

But now suppose you subclass WeakReference, and (additionally) store the referent in an instance variable in the subclass:

public class Frob<X> extends WeakReference<X> {
  private final X referent;
  public Frob(X referent) {
    super(referent);
    this.referent = referent;
  }
  // ...
}

Suppose further that at some point in the life of a program, the only remaining reference to the referent is this very instance variable (this.referent above).

To "get to" this variable, you have to "go through" the WeakReference subclass (Frob<X>). Granted, this kind of navigation is not via the get() method. Does this nevertheless still constitute traversal?

like image 215
Laird Nelson Avatar asked Jun 28 '26 01:06

Laird Nelson


1 Answers

The obvious conclusion is: to traverse a WeakReference, you call its get() method, yielding the referent. I understand that.

No. This "traverse" is not something you do, or even that the VM does, per se. It's part of an abstract description of characteristics of paths in a (notional) graph having objects as nodes and references of various kinds as directed edges. "Traverse" here evokes an analogy between a physical path and a "path" in the graph sense. We can imagine physically walking the path from one end to the other, traveling along -- "traversing" -- each edge in turn. A path in a graph traverses all the edges belonging to it. This is pretty idiomatic language for the domain.

As it applies to reachability analysis,

  • an object is strongly reachable if there is at least one path from a root to that object that consists entirely of edges representing strong references. Or equivalently, that traverses only edges representing strong references.

  • an object is softly reachable if it is not strongly reachable, but there is at least one path from a root to that object that consists entirely of (traverses only) edges representing strong references or soft references.

  • an object is weakly reachable if it is not strongly or softly reachable, but there is at least one path from a root to that object that traverses only edges representing strong, soft, or weak references.

  • analogously for phantom references, except except that an object must be finalized before it becomes phantom reachable.

If an object is weakly reachable then invoking the get() method of a WeakReference to it gives you a strong reference. That object is then strongly reachable, at least transiently, though it could become weakly reachable again later.

But now suppose you subclass WeakReference, and (additionally) store the referent in an instance variable in the subclass

Then the value stored in that instance variable, if not null, is a strong reference by definition. That's the only kind of reference you can touch directly. There is also a weak reference to the same object that is managed (in an unspecified manner) by the Frob / WeakReference. The ability to manage a weak reference is a function of the implementation of WeakReference and of the garbage collector, not of the identity of the WeakReference class.

Thus, if ...

To "get to" this variable, you have to "go through" the WeakReference subclass (Frob<X>) [...]. Does this nevertheless still constitute traversal?

That it is an object of a class descending from WeakReference that holds a strong reference to the object in question is immaterial. It's still a strong reference that it holds. If said reference object is itself strongly reachable, then any one of the existing paths of strong references that reaches it can be extended to a path of strong references that reaches the object in question, so that object is strongly reachable.

like image 160
John Bollinger Avatar answered Jun 29 '26 13:06

John Bollinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!