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 array darray and a String ident . It has a constructor using its String field. It overrides the toString() API method of Object to return its String field. It also overrides the finalize() 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, using poll() method of ReferenceQueue in its ReferenceQueue field. The method gets a Reference object if there is any available ,else null . If a Reference object is returned, it prints the Reference object cast to VeryBig 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 of ReferenceObjects class. Then we call the checkQueue() 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. The finalize() method is called only for weak references of VeryBig 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(); >> >
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.
Class Reference
Reference provides a way of recording address information about objects which themselves are not directly bound to the naming/directory system.
A Reference consists of an ordered list of addresses and class information about the object being referenced. Each address in the list identifies a communications endpoint for the same conceptual object. The «communications endpoint» is information that indicates how to contact the object. It could be, for example, a network address, a location in memory on the local machine, another process on the same machine, etc. The order of the addresses in the list may be of significance to object factories that interpret the reference.
Multiple addresses may arise for various reasons, such as replication or the object offering interfaces over more than one communication mechanism. The addresses are indexed starting with zero.
A Reference also contains information to assist in creating an instance of the object to which this Reference refers. It contains the class name of that object, and the class name and location of the factory to be used to create the object. The class factory location is a space-separated list of URLs representing the class path used to load the factory. When the factory class (or any class or resource upon which it depends) needs to be loaded, each URL is used (in order) to attempt to load the class.
A Reference instance is not synchronized against concurrent access by multiple threads. Threads that need to access a single Reference concurrently should synchronize amongst themselves and provide the necessary locking.
Class Reference
Reference provides a way of recording address information about objects which themselves are not directly bound to the naming/directory system.
A Reference consists of an ordered list of addresses and class information about the object being referenced. Each address in the list identifies a communications endpoint for the same conceptual object. The «communications endpoint» is information that indicates how to contact the object. It could be, for example, a network address, a location in memory on the local machine, another process on the same machine, etc. The order of the addresses in the list may be of significance to object factories that interpret the reference.
Multiple addresses may arise for various reasons, such as replication or the object offering interfaces over more than one communication mechanism. The addresses are indexed starting with zero.
A Reference also contains information to assist in creating an instance of the object to which this Reference refers. It contains the class name of that object, and the class name and location of the factory to be used to create the object. The class factory location is a space-separated list of URLs representing the class path used to load the factory. When the factory class (or any class or resource upon which it depends) needs to be loaded, each URL is used (in order) to attempt to load the class.
A Reference instance is not synchronized against concurrent access by multiple threads. Threads that need to access a single Reference concurrently should synchronize amongst themselves and provide the necessary locking.
Class Reference
Abstract base class for reference objects. This class defines the operations common to all reference objects. Because reference objects are implemented in close cooperation with the garbage collector, this class may not be subclassed directly.
Method Summary
This method was originally specified to test if a reference object has been cleared and enqueued but was never implemented to do this test.
Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method.
Methods declared in class java.lang.Object
Method Details
get
Returns this reference object’s referent. If this reference object has been cleared, either by the program or by the garbage collector, then this method returns null .
refersTo
Tests if the referent of this reference object is obj . Using a null obj returns true if the reference object has been cleared.
clear
Clears this reference object. Invoking this method will not cause this object to be enqueued. This method is invoked only by Java code; when the garbage collector clears references it does so directly, without invoking this method.
isEnqueued
This method was originally specified to test if a reference object has been cleared and enqueued but was never implemented to do this test. This method could be misused due to the inherent race condition or without an associated ReferenceQueue . An application relying on this method to release critical resources could cause serious performance issue. An application should use ReferenceQueue to reliably determine what reference objects that have been enqueued or refersTo(null) to determine if this reference object has been cleared.
- this reference object was registered with a queue when it was created; and
- the garbage collector has added this reference object to the queue or enqueue() is called; and
- this reference object is not yet removed from the queue.
enqueue
Clears this reference object and adds it to the queue with which it is registered, if any. This method is invoked only by Java code; when the garbage collector enqueues references it does so directly, without invoking this method.
clone
Throws CloneNotSupportedException . A Reference cannot be meaningfully cloned. Construct a new Reference instead.
reachabilityFence
Ensures that the object referenced by the given reference remains strongly reachable, regardless of any prior actions of the program that might otherwise cause the object to become unreachable; thus, the referenced object is not reclaimable by garbage collection at least until after the invocation of this method. Invocation of this method does not itself initiate garbage collection or finalization. This method establishes an ordering for strong reachability with respect to garbage collection. It controls relations that are otherwise only implicit in a program — the reachability conditions triggering garbage collection. This method is designed for use in uncommon situations of premature finalization where using synchronized blocks or methods, or using other synchronization facilities are not possible or do not provide the desired control. This method is applicable only when reclamation may have visible effects, which is possible for objects with finalizers (See Section 12.6 of The Java Language Specification ) that are implemented in ways that rely on ordering control for correctness.
API Note: Finalization may occur whenever the virtual machine detects that no reference to an object will ever be stored in the heap: The garbage collector may reclaim an object even if the fields of that object are still in use, so long as the object has otherwise become unreachable. This may have surprising and undesirable effects in cases such as the following example in which the bookkeeping associated with a class is managed through array indices. Here, method action uses a reachabilityFence to ensure that the Resource object is not reclaimed before bookkeeping on an associated ExternalResource has been performed; in particular here, to ensure that the array slot holding the ExternalResource is not nulled out in method Object.finalize() , which may otherwise run concurrently.
class Resource < private static ExternalResource[] externalResourceArray = . int myIndex; Resource(. ) < myIndex = . externalResourceArray[myIndex] = . ; . >protected void finalize() < externalResourceArray[myIndex] = null; . >public void action() < try < // . int i = myIndex; Resource.update(externalResourceArray[i]); >finally < Reference.reachabilityFence(this); >> private static void update(ExternalResource ext) < ext.status = . ; >>
Here, the invocation of reachabilityFence is nonintuitively placed after the call to update , to ensure that the array slot is not nulled out by Object.finalize() before the update, even if the call to action was the last use of this object. This might be the case if, for example a usage in a user program had the form new Resource().action(); which retains no other reference to this Resource . While probably overkill here, reachabilityFence is placed in a finally block to ensure that it is invoked across all paths in the method. In a method with more complex control paths, you might need further precautions to ensure that reachabilityFence is encountered along all of them. It is sometimes possible to better encapsulate use of reachabilityFence . Continuing the above example, if it were acceptable for the call to method update to proceed even if the finalizer had already executed (nulling out slot), then you could localize use of reachabilityFence :
public void action2() < // . Resource.update(getExternalResource()); >private ExternalResource getExternalResource()
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.