Different Reference Types
suggest changejava.lang.ref package provides reference-object classes, which support a limited degree of interaction with the garbage collector.
Java has four main different reference types. They are:
- Strong Reference
- Weak Reference
- Soft Reference
- Phantom Reference
1. Strong reference
This is the usual way to refer to an object.
MyObject myObject = new MyObject();
myObject holds a strong reference. As long as a strong reference to the instance is reachable, the garbage collector will not reclaim it.
2. Weak reference
A weak reference does not by itself keep the referent alive.
WeakReference<MyObject> myObjectRef = new WeakReference<MyObject>(new MyObject());
Simply, a weak reference is a reference that isn’t strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector’s ability to determine reachability for you, so you don’t have to do it yourself.
Call get() to read the referent. It returns null after the referent has been collected.
myObjectRef.get();
System.gc() only asks for a collection. A weak referent that is otherwise unreachable can disappear, but that is not guaranteed:
WeakReference<MyObject> myObjectRef = new WeakReference<MyObject>(new MyObject());
System.out.println(myObjectRef.get()); // the referent, or null
System.gc();
System.out.println(myObjectRef.get()); // null only if the collector cleared it
Printing the referent uses its toString(). The default toString looks like ClassName@hex; that hex is not a memory address the program can use.
3. Soft reference
A soft reference is cleared less aggressively than a weak reference. The collector may keep a softly reachable object while memory is plentiful.
SoftReference<MyObject> myObjectRef = new SoftReference<MyObject>(new MyObject());
They can hold onto the memory more strongly than the weak reference. If you have enough memory supply/resources, garbage collector will not clean the soft references as enthusiastically as weak references.
Soft references are handy to use in caching. You can create soft referenced objects as a cache, where they kept until your memory runs out. When your memory can’t supply enough resources, garbage collector will remove soft references.
SoftReference<MyObject> cached = new SoftReference<MyObject>(new MyObject());
System.out.println(cached.get()); // the referent, or null
System.gc();
System.out.println(cached.get()); // may still be the referent
4. Phantom reference
This is the weakest referencing type. If you created an object reference using Phantom Reference, the get() method will always return null!
The use of this referencing is that “Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism.” - From Phantom Reference Javadoc from Oracle.
PhantomReference has no one-argument constructor. The referent is registered with a queue, and get() always returns null:
ReferenceQueue<MyObject> queue = new ReferenceQueue<MyObject>();
PhantomReference<MyObject> myObjectRef =
new PhantomReference<MyObject>(new MyObject(), queue);