< prev index next >

src/java.base/share/classes/java/lang/ref/SoftReference.java

Print this page

  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  *








 34  * <p> Suppose that the garbage collector determines at a certain point in time
 35  * that an object is <a href="package-summary.html#reachability">softly
 36  * reachable</a>.  At that time it may choose to clear atomically all soft
 37  * references to that object and all soft references to any other
 38  * softly-reachable objects from which that object is reachable through a chain
 39  * of strong references.  At the same time or at some later time it will
 40  * enqueue those newly-cleared soft references that are registered with
 41  * reference queues.
 42  *
 43  * <p> All soft references to softly-reachable objects are guaranteed to have
 44  * been cleared before the virtual machine throws an
 45  * {@code OutOfMemoryError}.  Otherwise no constraints are placed upon the
 46  * time at which a soft reference will be cleared or the order in which a set
 47  * of such references to different objects will be cleared.  Virtual machine
 48  * implementations are, however, encouraged to bias against clearing
 49  * recently-created or recently-used soft references.
 50  *
 51  * <p> Direct instances of this class may be used to implement simple caches;
 52  * this class or derived subclasses may also be used in larger data structures
 53  * to implement more sophisticated caches.  As long as the referent of a soft

 64 
 65 public non-sealed class SoftReference<@jdk.internal.RequiresIdentity T> extends Reference<T> {
 66 
 67     /**
 68      * Timestamp clock, updated by the garbage collector
 69      */
 70     private static long clock;
 71 
 72     /**
 73      * Timestamp updated by each invocation of the get method.  The VM may use
 74      * this field when selecting soft references to be cleared, but it is not
 75      * required to do so.
 76      */
 77     private long timestamp;
 78 
 79     /**
 80      * Creates a new soft reference that refers to the given object.  The new
 81      * reference is not registered with any queue.
 82      *
 83      * @param referent object the new soft reference will refer to


 84      */
 85     public SoftReference(@jdk.internal.RequiresIdentity T referent) {
 86         super(referent);
 87         this.timestamp = clock;
 88     }
 89 
 90     /**
 91      * Creates a new soft reference that refers to the given object and is
 92      * registered with the given queue.
 93      *
 94      * @param referent object the new soft reference will refer to
 95      * @param q the queue with which the reference is to be registered,
 96      *          or {@code null} if registration is not required
 97      *

 98      */
 99     public SoftReference(@jdk.internal.RequiresIdentity T referent, ReferenceQueue<? super T> q) {
100         super(referent, q);
101         this.timestamp = clock;
102     }
103 
104     /**
105      * Returns this reference object's referent.  If this reference object has
106      * been cleared, either by the program or by the garbage collector, then
107      * this method returns {@code null}.
108      *
109      * @return   The object to which this reference refers, or
110      *           {@code null} if this reference object has been cleared
111      */
112     public T get() {
113         T o = super.get();
114         if (o != null && this.timestamp != clock)
115             this.timestamp = clock;
116         return o;
117     }

  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 import java.util.Objects;
 29 
 30 /**
 31  * Soft reference objects, which are cleared at the discretion of the garbage
 32  * collector in response to memory demand.  Soft references are most often used
 33  * to implement memory-sensitive caches.
 34  *
 35  * <div class="preview-block">
 36  *      <div class="preview-comment">
 37  *          The referent must have {@linkplain Objects#hasIdentity(Object) object identity}.
 38  *          When preview features are enabled, attempts to create a reference
 39  *          to a {@linkplain Class#isValue value object} result in an {@link IdentityException}.
 40  *      </div>
 41  * </div>
 42  *
 43  * <p> Suppose that the garbage collector determines at a certain point in time
 44  * that an object is <a href="package-summary.html#reachability">softly
 45  * reachable</a>.  At that time it may choose to clear atomically all soft
 46  * references to that object and all soft references to any other
 47  * softly-reachable objects from which that object is reachable through a chain
 48  * of strong references.  At the same time or at some later time it will
 49  * enqueue those newly-cleared soft references that are registered with
 50  * reference queues.
 51  *
 52  * <p> All soft references to softly-reachable objects are guaranteed to have
 53  * been cleared before the virtual machine throws an
 54  * {@code OutOfMemoryError}.  Otherwise no constraints are placed upon the
 55  * time at which a soft reference will be cleared or the order in which a set
 56  * of such references to different objects will be cleared.  Virtual machine
 57  * implementations are, however, encouraged to bias against clearing
 58  * recently-created or recently-used soft references.
 59  *
 60  * <p> Direct instances of this class may be used to implement simple caches;
 61  * this class or derived subclasses may also be used in larger data structures
 62  * to implement more sophisticated caches.  As long as the referent of a soft

 73 
 74 public non-sealed class SoftReference<@jdk.internal.RequiresIdentity T> extends Reference<T> {
 75 
 76     /**
 77      * Timestamp clock, updated by the garbage collector
 78      */
 79     private static long clock;
 80 
 81     /**
 82      * Timestamp updated by each invocation of the get method.  The VM may use
 83      * this field when selecting soft references to be cleared, but it is not
 84      * required to do so.
 85      */
 86     private long timestamp;
 87 
 88     /**
 89      * Creates a new soft reference that refers to the given object.  The new
 90      * reference is not registered with any queue.
 91      *
 92      * @param referent object the new soft reference will refer to
 93      * @throws IdentityException if the referent is not an
 94      *         {@link java.util.Objects#hasIdentity(Object) identity object}
 95      */
 96     public SoftReference(@jdk.internal.RequiresIdentity T referent) {
 97         super(referent);
 98         this.timestamp = clock;
 99     }
100 
101     /**
102      * Creates a new soft reference that refers to the given object and is
103      * registered with the given queue.
104      *
105      * @param referent object the new soft reference will refer to
106      * @param q the queue with which the reference is to be registered,
107      *          or {@code null} if registration is not required
108      * @throws IdentityException if the referent is not an
109      *         {@link java.util.Objects#hasIdentity(Object) identity object}
110      */
111     public SoftReference(@jdk.internal.RequiresIdentity T referent, ReferenceQueue<? super T> q) {
112         super(referent, q);
113         this.timestamp = clock;
114     }
115 
116     /**
117      * Returns this reference object's referent.  If this reference object has
118      * been cleared, either by the program or by the garbage collector, then
119      * this method returns {@code null}.
120      *
121      * @return   The object to which this reference refers, or
122      *           {@code null} if this reference object has been cleared
123      */
124     public T get() {
125         T o = super.get();
126         if (o != null && this.timestamp != clock)
127             this.timestamp = clock;
128         return o;
129     }
< prev index next >