Apart from the strong reference, the one used when you create a variable inside a function, Java allows for different types of reference. They can be used to tune memory management and garbage collection of temporary living objects, like cached objects or sessions.
Stron reference
Strong reference is created by assigning it to an object. As long as at least one reference to the object is present, the object will not get garbage-collected:
Object object = new Object();
Soft reference
When the GC will be be running out of memory, and the only reference to the object will be a soft reference, than this object will be garbage-collected.
Soft reference needs to be explicitly created, and the value needs to be specially retrieved. The get() function can return null, if the object was already GCed. Here is the sample code for soft reference usage:
Obj object = new Obj(); SoftReference softReferenceO= new SoftReference(object); Obj strongReferenceO = softReferenceO.get();
Weak reference
This one is almost like a soft reference, with the only difference, that the GC will mark the object eligible for garbage collection every time, when there is no other reference to it.
Weak reference needs to be explicitly created, and the value needs to be specially retrieved. The get() function can return null, if the object was already GCed. Here is the sample code for soft reference usage:
Obj object = new Obj(); WeakReference weakReferenceO= new WeakReference(object); Obj weakReferenceO = weakReferenceO.get();
WeakHashMap
Java provides, out of the box, a Map collection, where all the keys has weak references. That way, if the object is in the map, and its key has no other references that the one in the collection – it can be garbage-collected and removed from the map.
Sample WeakHashMap code:
WeakHashMap<Integer, Object> map = new WeakHashMap<>(); map.put(1, new Obj()); map.get(1);
Phantom reference
Phantom reference will always return null, then we try to retrieve the object. However, when the object is ready to be garbage-collected, after its finalize() method is run, it is added to the queue, specified in PhantomReference constructor. That way, we know when the object is destined for GC. Without the need to employ the cursed finalize, and thus without the possibility to resurrect the object or interfere with its life cycle.
Obj object = new Obj(); ReferenceQueue referenceQueue = new ReferenceQueue(); PhantomReference phantomReferenceO = new PhantomReference(object, referenceQueue); referenceQueue.remove();
