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