Search code examples
javacachingmemoryguavaheap-memory

google common cache - default value of maximumSize (and other "optional" settings) - want a cache that uses all "available" memory


I just found Guava by searching for a cache API (it fits perfectly for my needs). But one question arose on reading the wiki and Javadoc - what are the default values of settings the CacheBuilder can take? The Javadoc states "These features are all optional" and "Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind."

In my opinion, a good default for maximumSize would be relative to Runtime.getRuntime().freeMemory();

At the end I want a cache that uses the memory available on a given system. So I need an eviction strategy that asks how much freeMemory() is available (probably relative to Runtime.getRuntime().maxMemory())


Solution

  • I got me questioning the same thing and could not find anything on the web for that. So I made this very primitive test. I wrote a piece of code that creates a LocalCache with the most basic setup (no maximum size, no eviction policies, nothing) and in an infinite loop puts stuff in the cache. And monitored it through VisualVm to check the heap usage.

    import com.google.common.cache.Cache;
    import com.google.common.cache.CacheBuilder;
    
    import java.util.concurrent.TimeUnit;
    
    public class CacheTest {
        public static void main(String[] args) {
            Cache<String, String> cache = CacheBuilder.newBuilder().build();
            int counter = 0;
            while(true){
                cache.put("key"+counter++,"value");
                System.out.println("size:"+cache.size());
            }
        }
    }
    

    As you can see from the image below, the memory usage grows to the maximum available space and becomes constant. I waited for a few minutes and no OutOfMemoryError ocurred. What happened is that after a few seconds one new entry is added to the map so there will be probably an error in the future.

    Heap Dump

    Conclusion: You don't have to set the maximumSize value, but I suggest you use some kind of eviction policy (expireAfterAccess or expireAfterWrite) to clean up the cache and avoid an OutOfMemoryError. And also to avoid degrading the performance of your cache.