< prev index next >

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

Print this page

  1 /*
  2  * Copyright (c) 1997, 2003, 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  *
 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

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


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

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

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

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