Skip to content

Commit

Permalink
GROOVY-11271: ConcurrentCommonCache causes memory leaks
Browse files Browse the repository at this point in the history
(cherry picked from commit 43f5630)
  • Loading branch information
daniellansun committed Jan 19, 2024
1 parent c6f8133 commit b9b321a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.codehaus.groovy.runtime.memoize;

import org.apache.groovy.util.concurrent.concurrentlinkedhashmap.ConcurrentLinkedHashMap;

import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -64,14 +66,26 @@ public CommonCache() {
* @param evictionStrategy LRU or FIFO, see {@link org.codehaus.groovy.runtime.memoize.EvictableCache.EvictionStrategy}
*/
public CommonCache(final int initialCapacity, final int maxSize, final EvictionStrategy evictionStrategy) {
this(new LinkedHashMap<K, V>(initialCapacity, DEFAULT_LOAD_FACTOR, EvictionStrategy.LRU == evictionStrategy) {
this(createMap(initialCapacity, maxSize, evictionStrategy));
}

private static <K, V> Map<K, V> createMap(int initialCapacity, int maxSize, EvictionStrategy evictionStrategy) {
final boolean lru = EvictionStrategy.LRU == evictionStrategy;

if (lru) {
return new ConcurrentLinkedHashMap.Builder<K, V>()
.initialCapacity(initialCapacity)
.maximumWeightedCapacity(maxSize)
.build();
}
return new LinkedHashMap<K, V>(initialCapacity, DEFAULT_LOAD_FACTOR, lru) {
private static final long serialVersionUID = -8012450791479726621L;

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
});
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void testLruCache() {
sc.put("c", "3");
sc.put("a", "4");
sc.put("d", "5");
Assert.assertArrayEquals(new String[] {"c", "a", "d"}, sc.keys().toArray(new String[0]));
Assert.assertEquals(3, sc.size());
Assert.assertEquals("3", sc.get("c"));
Assert.assertEquals("4", sc.get("a"));
Assert.assertEquals("5", sc.get("d"));
Expand All @@ -196,4 +196,4 @@ public void testFifoCache() {
Assert.assertEquals("3", sc.get("c"));
Assert.assertEquals("5", sc.get("d"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testLruCache() {
sc.put("c", "3");
sc.put("a", "4");
sc.put("d", "5");
Assert.assertArrayEquals(new String[] {"c", "a", "d"}, sc.keys().toArray(new String[0]));
Assert.assertEquals(3, sc.size());
Assert.assertEquals("3", sc.get("c"));
Assert.assertEquals("4", sc.get("a"));
Assert.assertEquals("5", sc.get("d"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testLruCache() {
sc.put("c", "3");
sc.put("a", "4");
sc.put("d", "5");
Assert.assertArrayEquals(new String[] {"c", "a", "d"}, sc.keys().toArray(new String[0]));
Assert.assertEquals(3, sc.size());
Assert.assertEquals("3", sc.get("c"));
Assert.assertEquals("4", sc.get("a"));
Assert.assertEquals("5", sc.get("d"));
Expand Down

0 comments on commit b9b321a

Please sign in to comment.