Skip to content
This repository has been archived by the owner on Jan 14, 2024. It is now read-only.

aristotle0x01/java_reference

Repository files navigation

java_reference

on java soft, weak and phantom reference

java引用类层次 image

Reference Queue

ReferenceQueue:

Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.

What does a reachability change mean ?

We know that the Garbage collector will free up any weak reference objects or weakly reachable objects as a part of the garbage collection process. The flow for the same is: If the garbage collector discovers an object that is weakly reachable, the following occurs:

  1. The WeakReference object's referent field is set to null, thereby making it not refer to the heap object any longer.
  2. The heap object that had been referenced by the WeakReference is declared finalizable.
  3. The WeakReference object is added to its ReferenceQueue. Then the heap object's finalize() method is run and its memory freed.

So what is this ReferenceQueue ? When creating non-strong reference objects we have the option of passing a reference queue as a part of the Reference constructor. As seen from the above explanation, this reference queue is a way for the GC to inform the program that a certain object is no longer reachable.

Reference Queues

Reference Objects and Garbage Collection

Reference Objects and Garbage Collection

非常好的文章,从垃圾收集与各种引用的关系来说明,非常清晰透彻

java gc的可达性说明

image

弱引用说明

image

强弱共存

image

Phantom Reference

Java Garbage Collection - Understanding Phantom Reference with examples

Features of PhantomReference

About

on java soft, weak and phantom reference

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages