Reference Objects example
In this example we shall show you how to create and use Reference Objects. A reference object encapsulates a reference to some other object so that the reference itself may be examined and manipulated like any other object. Three types of reference objects are provided, each weaker than the last: soft, weak, and phantom. Each type corresponds to a different level of reachability. Soft references are for implementing memory-sensitive caches, weak references are for implementing canonicalizing mappings that do not prevent their keys (or values) from being reclaimed, and phantom references are for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. Each reference-object type is implemented by a subclass of the abstract base Reference class. An instance of one of these subclasses encapsulates a single reference to a particular object, called the referent. Every reference object provides methods for getting and clearing the reference. Aside from the clearing operation reference objects are otherwise immutable, so no set operation is provided. In the example we have created a class, VeryBig
and created reference objects of all types, using a class with a ReferenceQueue, following the below steps:
VeryBig
class has a constant field,SIZE
and two fields, a double arraydarray
and a Stringident
. It has a constructor using its String field. It overrides thetoString()
API method of Object to return its String field. It also overrides thefinalize()
API method of Object to dispose of System resources.ReferenceObjects
class has a static ReferenceQueue field and a static method,void checkQueue()
that creates a new object, usingpoll()
method of ReferenceQueue in its ReferenceQueue field. The method gets a Reference object if there is any available ,elsenull
. If a Reference object is returned, it prints the Reference object cast toVeryBig
class.- We create a new SoftReference array with a given size. For each one of its fields we create a new SoftReference, that refers to a new
VeryBig
object and is registered with the ReferenceQueue field ofReferenceObjects
class. Then we call thecheckQueue()
method of the class. - We follow the same steps, but this time creating a new WeakReference array.
- Then we call the
System.gc()
to run the garbage collector that will clear all objects and their references. Thefinalize()
method is called only for weak references ofVeryBig
object. - We create a new PhantomReference array and follow the same steps as in the above arrays. The PhantomReference get method always returns null,
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.lang.ref.*; class VeryBig { private static final int SIZE = 10000; private double[] darray = new double[SIZE]; private String ident; public VeryBig(String id) { ident = id; } @Override public String toString() { return ident; } @Override public void finalize() { System.out.println("Finalizing " + ident); } } public class ReferenceObjects { private static ReferenceQueue rq = new ReferenceQueue(); public static void checkQueue() { Object obj = rq.poll(); if (obj != null) { System.out.println("In queue: " + (VeryBig) ((Reference) obj).get()); } } public static void main(String[] args) { int size = 10; // Or, choose size via the command line: if (args.length > 0) { size = Integer.parseInt(args[0]); } SoftReference[] sa = new SoftReference[size]; for (int i = 0; i < sa.length; i++) { sa[i] = new SoftReference(new VeryBig("Soft " + i), rq); System.out.println("Just created: " + (VeryBig) sa[i].get()); checkQueue(); } WeakReference[] wa = new WeakReference[size]; for (int i = 0; i < wa.length; i++) { wa[i] = new WeakReference(new VeryBig("Weak " + i), rq); System.out.println("Just created: " + (VeryBig) wa[i].get()); checkQueue(); } SoftReference s = new SoftReference(new VeryBig("Soft")); WeakReference w = new WeakReference(new VeryBig("Weak")); System.gc(); PhantomReference[] pa = new PhantomReference[size]; for (int i = 0; i < pa.length; i++) { pa[i] = new PhantomReference(new VeryBig("Phantom " + i), rq); System.out.println("Just created: " + (VeryBig) pa[i].get()); checkQueue(); } } }
Output:
Just created: Soft 0
Just created: Soft 1
Just created: Soft 2
Just created: Soft 3
Just created: Weak 0
Just created: Weak 1
Just created: Weak 2
Just created: Weak 3
Just created: null
In queue: null
Just created: null
In queue: null
Finalizing Weak 0
Finalizing Weak
Finalizing Weak 3
Just created: null
In queue: null
Just created: null
In queue: null
Finalizing Weak 2
Finalizing Weak 1
This was an example of how to create and use Reference Objects in Java.