1. /*
  2. * @(#)SoftReference.java 1.30 03/01/23
  3. *
  4. * Copyright 2003 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.30, 01/23/03
  40. * @author Mark Reinhold
  41. * @since 1.2
  42. */
  43. public class SoftReference extends Reference {
  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(Object 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 queue the soft reference is registered with
  68. * @throws NullPointerException If the <code>queue</code> argument
  69. * is <code>null</code>
  70. *
  71. */
  72. public SoftReference(Object referent, ReferenceQueue q) {
  73. super(referent, q);
  74. this.timestamp = clock;
  75. }
  76. /**
  77. * Returns this reference object's referent. If this reference object has
  78. * been cleared, either by the program or by the garbage collector, then
  79. * this method returns <code>null</code>.
  80. *
  81. * @return The object to which this reference refers, or
  82. * <code>null</code> if this reference object has been cleared
  83. */
  84. public Object get() {
  85. Object o = super.get();
  86. if (o != null) this.timestamp = clock;
  87. return o;
  88. }
  89. }