|
20 | 20 |
|
21 | 21 | import java.lang.ref.Reference;
|
22 | 22 | import java.lang.ref.ReferenceQueue;
|
23 |
| -import java.lang.ref.SoftReference; |
| 23 | +import java.lang.ref.WeakReference; |
24 | 24 | import java.util.HashSet;
|
25 | 25 | import java.util.Objects;
|
26 | 26 | import java.util.Set;
|
|
29 | 29 | import java.util.function.Function;
|
30 | 30 |
|
31 | 31 | /**
|
32 |
| - * Default implementation of Cache that uses soft references for both keys and values, |
| 32 | + * Default implementation of Cache that uses weak references for both keys and values, |
33 | 33 | * with identity-based key comparison for better performance.
|
34 | 34 | *
|
35 | 35 | * <p>This implementation is thread-safe and automatically cleans up garbage-collected
|
36 |
| - * entries. It uses soft references to allow garbage collection under memory pressure |
37 |
| - * while providing memory benefits for typical usage patterns. |
| 36 | + * entries. It uses weak references to allow garbage collection when objects are no longer |
| 37 | + * strongly referenced, preventing memory leaks while providing caching benefits. |
38 | 38 | *
|
39 | 39 | * @param <K> the type of keys maintained by this cache
|
40 | 40 | * @param <V> the type of cached values
|
@@ -190,9 +190,9 @@ private void expungeStaleEntries() {
|
190 | 190 | }
|
191 | 191 |
|
192 | 192 | /**
|
193 |
| - * Soft reference wrapper for keys that uses identity-based equality. |
| 193 | + * Weak reference wrapper for keys that uses identity-based equality. |
194 | 194 | */
|
195 |
| - private static class KeyReference<K> extends SoftReference<K> { |
| 195 | + private static class KeyReference<K> extends WeakReference<K> { |
196 | 196 | private final int hashCode;
|
197 | 197 |
|
198 | 198 | KeyReference(K key, ReferenceQueue<K> queue) {
|
@@ -221,9 +221,9 @@ public int hashCode() {
|
221 | 221 | }
|
222 | 222 |
|
223 | 223 | /**
|
224 |
| - * Soft reference wrapper for values with computation state tracking. |
| 224 | + * Weak reference wrapper for values with computation state tracking. |
225 | 225 | */
|
226 |
| - private static class ValueReference<V> extends SoftReference<V> { |
| 226 | + private static class ValueReference<V> extends WeakReference<V> { |
227 | 227 | private final boolean computing;
|
228 | 228 |
|
229 | 229 | ValueReference(V value, ReferenceQueue<V> queue) {
|
|
0 commit comments