1 /*
  2  * Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang.ref;
 27 
 28 
 29 /**
 30  * Soft reference objects, which are cleared at the discretion of the garbage
 31  * collector in response to memory demand.  Soft references are most often used
 32  * to implement memory-sensitive caches.
 33  * <p>
 34  * The referent must not be an instance of a {@linkplain Class#isValue()
 35  * value class}; such a value can never have another reference to it
 36  * and cannot be held in a reference type.
 37  *
 38  * <p> Suppose that the garbage collector determines at a certain point in time
 39  * that an object is <a href="package-summary.html#reachability">softly
 40  * reachable</a>.  At that time it may choose to clear atomically all soft
 41  * references to that object and all soft references to any other
 42  * softly-reachable objects from which that object is reachable through a chain
 43  * of strong references.  At the same time or at some later time it will
 44  * enqueue those newly-cleared soft references that are registered with
 45  * reference queues.
 46  *
 47  * <p> All soft references to softly-reachable objects are guaranteed to have
 48  * been cleared before the virtual machine throws an
 49  * {@code OutOfMemoryError}.  Otherwise no constraints are placed upon the
 50  * time at which a soft reference will be cleared or the order in which a set
 51  * of such references to different objects will be cleared.  Virtual machine
 52  * implementations are, however, encouraged to bias against clearing
 53  * recently-created or recently-used soft references.
 54  *
 55  * <p> Direct instances of this class may be used to implement simple caches;
 56  * this class or derived subclasses may also be used in larger data structures
 57  * to implement more sophisticated caches.  As long as the referent of a soft
 58  * reference is strongly reachable, that is, is actually in use, the soft
 59  * reference will not be cleared.  Thus a sophisticated cache can, for example,
 60  * prevent its most recently used entries from being discarded by keeping
 61  * strong referents to those entries, leaving the remaining entries to be
 62  * discarded at the discretion of the garbage collector.
 63  *
 64  * @author   Mark Reinhold
 65  * @since    1.2
 66  */
 67 
 68 public non-sealed class SoftReference<T> extends Reference<T> {
 69 
 70     /**
 71      * Timestamp clock, updated by the garbage collector
 72      */
 73     private static long clock;
 74 
 75     /**
 76      * Timestamp updated by each invocation of the get method.  The VM may use
 77      * this field when selecting soft references to be cleared, but it is not
 78      * required to do so.
 79      */
 80     private long timestamp;
 81 
 82     /**
 83      * Creates a new soft reference that refers to the given object.  The new
 84      * reference is not registered with any queue.
 85      *
 86      * @param referent object the new soft reference will refer to
 87      * @throws IllegalArgumentException if the referent is an instance of a
 88      *         {@link Class#isValue() value class}
 89      */
 90     public SoftReference(T referent) {
 91         super(referent);
 92         this.timestamp = clock;
 93     }
 94 
 95     /**
 96      * Creates a new soft reference that refers to the given object and is
 97      * registered with the given queue.
 98      *
 99      * @param referent object the new soft reference will refer to
100      * @param q the queue with which the reference is to be registered,
101      *          or {@code null} if registration is not required
102      * @throws IllegalArgumentException if the referent is an instance of a
103      *         {@link Class#isValue() value class}
104      */
105     public SoftReference(T referent, ReferenceQueue<? super T> q) {
106         super(referent, q);
107         this.timestamp = clock;
108     }
109 
110     /**
111      * Returns this reference object's referent.  If this reference object has
112      * been cleared, either by the program or by the garbage collector, then
113      * this method returns {@code null}.
114      *
115      * @return   The object to which this reference refers, or
116      *           {@code null} if this reference object has been cleared
117      */
118     public T get() {
119         T o = super.get();
120         if (o != null && this.timestamp != clock)
121             this.timestamp = clock;
122         return o;
123     }
124 
125 }