SOLR-1798: fix FastLRUCache stats memory leak

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@919871 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-03-06 22:49:24 +00:00
parent ab9a5ca386
commit 4f5166fdae
3 changed files with 29 additions and 12 deletions

View File

@ -192,6 +192,9 @@ Bug Fixes
* SOLR-1777: fieldTypes with sortMissingLast=true or sortMissingFirst=true can
result in incorrectly sorted results. (yonik)
* SOLR-1798: Small memory leak (~100 bytes) in fastLRUCache for every
commit. (yonik)
Other Changes
----------------------

View File

@ -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>{

View File

@ -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();