do { remove(lastAccessedList.getLast().object); } while (cacheSize > desiredSize); t = System.currentTimeMillis() - t;
/** * Removes objects from cache if the cache is too full. "Too full" is * defined as within 3% of the maximum cache size. Whenever the cache is * is too big, the least frequently used elements are deleted until the * cache is at least 10% empty. */ protected final void cullCache() { // Check if a max cache size is defined. if (maxCacheSize < 0) { return; } // See if the cache size is within 3% of being too big. If so, clean out // cache until it's 10% free. int desiredSize = (int)(maxCacheSize * .97); if (cacheSize >= desiredSize) { // First, delete any old entries to see how much memory that frees. deleteExpiredEntries(); desiredSize = (int)(maxCacheSize * .90); if (cacheSize > desiredSize) { long t = System.currentTimeMillis(); do { // Get the key and invoke the remove method on it. remove(lastAccessedList.getLast().object); } while (cacheSize > desiredSize); t = System.currentTimeMillis() - t; Log.warn("Cache " + name + " was full, shrinked to 90% in " + t + "ms."); } } }