Can PhantomReference stop the referent from recovering memory?

My question that sums it all up is:

  • Can a highly accessible Java PhantomReference stop the memory of a reference object from being reclaimed by a garbage collector (GC)?

Details follow:

Callum posted this question , but it was not answered directly. One answer there refers to an article by Ethan Nicholas , which seems to answer my question “No,” but I'm not sure if this is correct.

Based on my reading of the Java API, I would have to answer my question with “Yes”:

  • Until PhantomReference.clear () is called, and the PhantomReference instance itself is still strongly referenced, the memory of the referent object will never be restored, and the referent will remain in phantom accessibility state.

To support this understanding, I will cite Java Docs :

  • “Unlike soft and weak links, phantom links are not automatically cleared by the garbage collector because they are in the queue. The object accessible via phantom links will remain the same until all such links are cleared or become inaccessible .

For example, let's say I create a phantom link and save this instance in the PhantomReference list. Then his referent falls from the attainable to the phantom of the attainable.

com.google.common.base.internal.Finalizer.java, :

  private void cleanUp(Reference reference) throws ShutDown {
      ...

      /*
       * This is for the benefit of phantom references. Weak and soft
       * references will have already been cleared by this point.
       */
      reference.clear();

      ...
  }

, -, , , - . !

+4
1

. , PhantomReference. PhantomReference, , , , . :

. , , . , ​​ . , , .

. , , .

. Java 8 PhantomReference , phantom , , . , , , , PhantomReference, , .


. , phantom , . , .

, Java 9 phantom . , Java 9, phantom , , , , .


:

... , phantom PhantomReference. phantom .

PhantomReference, , phantom . , , , , finalize(). phantom, , phantom.

+2

All Articles