1. /*
  2. * @(#)SoftReference.java 1.34 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.ref;
  8. /**
  9. * Soft reference objects, which are cleared at the discretion of the garbage
  10. * collector in response to memory demand. Soft references are most often used
  11. * to implement memory-sensitive caches.
  12. *
  13. * <p> Suppose that the garbage collector determines at a certain point in time
  14. * that an object is <a href="package-summary.html#reachability">softly
  15. * reachable</a>. At that time it may choose to clear atomically all soft
  16. * references to that object and all soft references to any other
  17. * softly-reachable objects from which that object is reachable through a chain
  18. * of strong references. At the same time or at some later time it will
  19. * enqueue those newly-cleared soft references that are registered with
  20. * reference queues.
  21. *
  22. * <p> All soft references to softly-reachable objects are guaranteed to have
  23. * been cleared before the virtual machine throws an
  24. * <code>OutOfMemoryError</code>. Otherwise no constraints are placed upon the
  25. * time at which a soft reference will be cleared or the order in which a set
  26. * of such references to different objects will be cleared. Virtual machine
  27. * implementations are, however, encouraged to bias against clearing
  28. * recently-created or recently-used soft references.
  29. *
  30. * <p> Direct instances of this class may be used to implement simple caches;
  31. * this class or derived subclasses may also be used in larger data structures
  32. * to implement more sophisticated caches. As long as the referent of a soft
  33. * reference is strongly reachable, that is, is actually in use, the soft
  34. * reference will not be cleared. Thus a sophisticated cache can, for example,
  35. * prevent its most recently used entries from being discarded by keeping
  36. * strong referents to those entries, leaving the remaining entries to be
  37. * discarded at the discretion of the garbage collector.
  38. *
  39. * @version 1.34, 12/19/03
  40. * @author Mark Reinhold
  41. * @since 1.2
  42. */
  43. public class SoftReference<T> extends Reference<T> {
  44. /* Timestamp clock, updated by the garbage collector
  45. */
  46. static private long clock;
  47. /* Timestamp updated by each invocation of the get method. The VM may use
  48. * this field when selecting soft references to be cleared, but it is not
  49. * required to do so.
  50. */
  51. private long timestamp;
  52. /**
  53. * Creates a new soft reference that refers to the given object. The new
  54. * reference is not registered with any queue.
  55. *
  56. * @param referent object the new soft reference will refer to
  57. */
  58. public SoftReference(T referent) {
  59. super(referent);
  60. this.timestamp = clock;
  61. }
  62. /**
  63. * Creates a new soft reference that refers to the given object and is
  64. * registered with the given queue.
  65. *
  66. * @param referent object the new soft reference will refer to
  67. * @param q the queue with which the reference is to be registered,
  68. * or <tt>null</tt> if registration is not required
  69. *
  70. */
  71. public SoftReference(T referent, ReferenceQueue<? super T> q) {
  72. super(referent, q);
  73. this.timestamp = clock;
  74. }
  75. /**
  76. * Returns this reference object's referent. If this reference object has
  77. * been cleared, either by the program or by the garbage collector, then
  78. * this method returns <code>null</code>.
  79. *
  80. * @return The object to which this reference refers, or
  81. * <code>null</code> if this reference object has been cleared
  82. */
  83. public T get() {
  84. T o = super.get();
  85. if (o != null) this.timestamp = clock;
  86. return o;
  87. }
  88. }