1. /*
  2. * @(#)SoftReference.java 1.24 01/11/29
  3. *
  4. * Copyright 2002 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.24, 01/11/29
  40. * @author Mark Reinhold
  41. * @since JDK1.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. public SoftReference(Object referent) {
  57. super(referent);
  58. this.timestamp = clock;
  59. }
  60. /**
  61. * Creates a new soft reference that refers to the given object and is
  62. * registered with the given queue.
  63. *
  64. * @throws NullPointerException If the <code>queue</code> argument
  65. * is <code>null</code>
  66. *
  67. */
  68. public SoftReference(Object referent, ReferenceQueue q) {
  69. super(referent, q);
  70. this.timestamp = clock;
  71. }
  72. /**
  73. * Returns this reference object's referent. If this reference object has
  74. * been cleared, either by the program or by the garbage collector, then
  75. * this method returns <code>null</code>.
  76. *
  77. * @return The object to which this reference refers, or
  78. * <code>null</code> if this reference object has been cleared
  79. */
  80. public Object get() {
  81. Object o = super.get();
  82. if (o != null) this.timestamp = clock;
  83. return o;
  84. }
  85. }