Skip to content

Commit ceae74f

Browse files
committed
Fix OOM issues by switching from SOFT to WEAK references
Analysis of the working PR #2506 revealed that the key difference is the use of WEAK references instead of SOFT references for cache entries. Root Cause: - SOFT references are only cleared when JVM is close to OutOfMemoryError - This causes excessive memory retention and can lead to OOM in large builds - WEAK references are cleared much more aggressively when objects are no longer strongly referenced, preventing memory buildup Changes: - Switch from SoftReference to WeakReference for both keys and values - Update documentation to reflect the change from soft to weak references - This aligns with the working PR #2506 which uses Cache.ReferenceType.WEAK Benefits: - Prevents OOM issues in integration tests and large builds - Maintains caching benefits while allowing more aggressive garbage collection - Follows the proven approach from the comprehensive PR #2506 All 12 cache tests continue to pass with this change.
1 parent 1bff33c commit ceae74f

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/cache/Cache.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
* A simple cache interface for storing key-value pairs with automatic cleanup
2525
* of garbage-collected entries.
2626
*
27-
* <p>This cache uses soft references to allow garbage collection under memory
28-
* pressure while providing memory benefits for typical usage patterns.
27+
* <p>This cache uses weak references to allow garbage collection when objects
28+
* are no longer strongly referenced, preventing memory leaks while providing
29+
* caching benefits for typical usage patterns.
2930
*
3031
* @param <K> the type of keys maintained by this cache
3132
* @param <V> the type of cached values

impl/maven-impl/src/main/java/org/apache/maven/impl/cache/DefaultCache.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import java.lang.ref.Reference;
2222
import java.lang.ref.ReferenceQueue;
23-
import java.lang.ref.SoftReference;
23+
import java.lang.ref.WeakReference;
2424
import java.util.HashSet;
2525
import java.util.Objects;
2626
import java.util.Set;
@@ -29,12 +29,12 @@
2929
import java.util.function.Function;
3030

3131
/**
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,
3333
* with identity-based key comparison for better performance.
3434
*
3535
* <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.
3838
*
3939
* @param <K> the type of keys maintained by this cache
4040
* @param <V> the type of cached values
@@ -190,9 +190,9 @@ private void expungeStaleEntries() {
190190
}
191191

192192
/**
193-
* Soft reference wrapper for keys that uses identity-based equality.
193+
* Weak reference wrapper for keys that uses identity-based equality.
194194
*/
195-
private static class KeyReference<K> extends SoftReference<K> {
195+
private static class KeyReference<K> extends WeakReference<K> {
196196
private final int hashCode;
197197

198198
KeyReference(K key, ReferenceQueue<K> queue) {
@@ -221,9 +221,9 @@ public int hashCode() {
221221
}
222222

223223
/**
224-
* Soft reference wrapper for values with computation state tracking.
224+
* Weak reference wrapper for values with computation state tracking.
225225
*/
226-
private static class ValueReference<V> extends SoftReference<V> {
226+
private static class ValueReference<V> extends WeakReference<V> {
227227
private final boolean computing;
228228

229229
ValueReference(V value, ReferenceQueue<V> queue) {

0 commit comments

Comments
 (0)