Apache Solr open-source search software
SOLR-1798: fix FastLRUCache stats memory leak
Yonik Seeley4f5166fdaefd
diff --git a/CHANGES.txt b/CHANGES.txtindex b6c216449d2..4facfe38146 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -192,6 +192,9 @@ Bug Fixes* SOLR-1777: fieldTypes with sortMissingLast=true or sortMissingFirst=true canresult in incorrectly sorted results. (yonik)+* SOLR-1798: Small memory leak (~100 bytes) in fastLRUCache for every+ commit. (yonik)+Other Changes----------------------diff --git a/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java b/src/common/org/apache/solr/common/util/ConcurrentLRUCache.javaindex 19df0acda19..52b025642f4 100644--- a/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java+++ b/src/common/org/apache/solr/common/util/ConcurrentLRUCache.java@@ -384,7 +384,7 @@ public class ConcurrentLRUCache<K,V> {CacheEntry<K,V> o = map.remove(key);if (o == null) return;stats.size.decrementAndGet();- stats.evictionCounter++;+ stats.evictionCounter.incrementAndGet();if(evictionListener != null) evictionListener.evictedEntry(o.key,o.value);}@@ -518,7 +518,7 @@ public class ConcurrentLRUCache<K,V> {nonLivePutCounter = new AtomicLong(0),missCounter = new AtomicLong();private final AtomicInteger size = new AtomicInteger();- private long evictionCounter = 0;+ private AtomicLong evictionCounter = new AtomicLong();public long getCumulativeLookups() {return (accessCounter.get() - putCounter.get() - nonLivePutCounter.get()) + missCounter.get();@@ -533,7 +533,7 @@ public class ConcurrentLRUCache<K,V> {}public long getCumulativeEvictions() {- return evictionCounter;+ return evictionCounter.get();}public int getCurrentSize() {@@ -547,6 +547,15 @@ public class ConcurrentLRUCache<K,V> {public long getCumulativeMisses() {return missCounter.get();}++ public void add(Stats other) {+ accessCounter.addAndGet(other.accessCounter.get());+ putCounter.addAndGet(other.putCounter.get());+ nonLivePutCounter.addAndGet(other.nonLivePutCounter.get());+ missCounter.addAndGet(other.missCounter.get());+ evictionCounter.addAndGet(other.evictionCounter.get());+ size.set(Math.max(size.get(), other.size.get()));+ }}public static interface EvictionListener<K,V>{diff --git a/src/java/org/apache/solr/search/FastLRUCache.java b/src/java/org/apache/solr/search/FastLRUCache.javaindex ffa7fc5c0a9..1632e5810de 100644--- a/src/java/org/apache/solr/search/FastLRUCache.java+++ b/src/java/org/apache/solr/search/FastLRUCache.java@@ -45,7 +45,8 @@ import java.util.concurrent.CopyOnWriteArrayList;*/public class FastLRUCache<K,V> implements SolrCache<K,V> {- private List<ConcurrentLRUCache.Stats> cumulativeStats;+ // contains the statistics objects for all open caches of the same type+ private List<ConcurrentLRUCache.Stats> statsList;private long warmupTime = 0;@@ -104,16 +105,18 @@ public class FastLRUCache<K,V> implements SolrCache<K,V> {cache = new ConcurrentLRUCache<K,V>(limit, minLimit, acceptableLimit, initialSize, newThread, false, null);cache.setAlive(false);- if (persistence == null) {+ statsList = (List<ConcurrentLRUCache.Stats>) persistence;+ if (statsList == null) {// must be the first time a cache of this type is being created// Use a CopyOnWriteArrayList since puts are very rare and iteration may be a frequent operation// because it is used in getStatistics()- persistence = new CopyOnWriteArrayList<ConcurrentLRUCache.Stats>();- }+ statsList = new CopyOnWriteArrayList<ConcurrentLRUCache.Stats>();- cumulativeStats = (List<ConcurrentLRUCache.Stats>) persistence;- cumulativeStats.add(cache.getStats());- return cumulativeStats;+ // the first entry will be for cumulative stats of caches that have been closed.+ statsList.add(new ConcurrentLRUCache.Stats());+ }+ statsList.add(cache.getStats());+ return statsList;}public String name() {@@ -131,7 +134,6 @@ public class FastLRUCache<K,V> implements SolrCache<K,V> {public V get(K key) {return cache.get(key);-}public void clear() {@@ -177,6 +179,9 @@ public class FastLRUCache<K,V> implements SolrCache<K,V> {public void close() {+ // add the stats to the cumulative stats object (the first in the statsList)+ statsList.get(0).add(cache.getStats());+ statsList.remove(cache.getStats());cache.destroy();}@@ -243,7 +248,7 @@ public class FastLRUCache<K,V> implements SolrCache<K,V> {long cevictions = 0;// NOTE: It is safe to iterate on a CopyOnWriteArrayList- for (ConcurrentLRUCache.Stats statistiscs : cumulativeStats) {+ for (ConcurrentLRUCache.Stats statistiscs : statsList) {clookups += statistiscs.getCumulativeLookups();chits += statistiscs.getCumulativeHits();cinserts += statistiscs.getCumulativePuts();