private 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.
        long desiredSize = (long)(maxCacheSize * .97);
        if (cacheSize >= desiredSize) {
            // First, delete any old entries to see how much memory that frees.
            deleteExpiredEntries();
            desiredSize = (long) (maxCacheSize * .90);
            if (cacheSize > desiredSize) {
                long t = System.currentTimeMillis();
                cullTimes.add(t);
                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, shrunk to 90% in " + t + "ms.");
            }
        }

        cullCacheTimes();
    }
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.
xmppserver/src/main/java/org/jivesoftware/util/cache/DefaultCache.java