3. What is “use”
● An object can be reached by a program in its current state
Main Thread
forked Threads
RunnClass#
runningMethod..
Root set of reference
static reference
JNI API registered
references
Reachable
4. Garbage Collection
● All other than reachable objects are unreachable
● Unreachable objects are eligible for garbage collection
Red box are
unreachable objects
5. Reference object
● Reference objects will be treated differently by GC
● Concept: Refernece and Referent
SoftReference Image
Reference Referent
6. Object Reachability
● Reference Objects API gives us strengths of object reachability
○ Strongly Referent
○ Softly Reference
○ Weakly Reference
○ Phantomly Reference
● By this API we can get a limited amount of interaction with GC
according to the strength of the object’s reachability
8. Weak Reachable
● The only way to reach an object involve at least one weak reference object
● Weak referenced objects are eligible for collection
● Weak reference object allow us to refer to and object without keeping it from
being collected
● When a referenced object is
collected, the value of
WeakReference will be set
to null
● Weak reference life time
won’t be extended
9. Softly Reachable
● Not a strongly reachable
● No weak reference
● No Phantom reference
● One or more soft reference
● GC must clear all soft reference before throwing OutOfMemoryError
● GC will clear it when memory is not enough
● GC may not clear it when it’s used recently, life time may be extended
● Often used to implement a cache
10. Phantomly Reachable
● When an object has no weak reference and soft reference, or become
unreachable by itself. The object will be phantomly referenced
● PhantomReference has a constructor: ReferenceQueue. It can be used to
monitor finialization status controlled by GC.
11. ReferenceQueue
● A data structure for you to find out when an object becomes softly, weakly, or
phantomly reachable.