13
votes

I'm trying to figure out what the number that you specify in Guava CacheBuilder maximumSize() represent.

Say I've got something like this in my code,

Cache<String, Object> programCache = CacheBuilder.newBuilder()
    .maximumSize(1000)
    .build();

Does the 1000 that I specified as the max size mean that I can have a thousand different entries in the cache before it starts kicking out the LRU (no matter what size the object might be)? If this is the case, is there a limit to the size of the object?

Or does that 1000 mean, that I have a 1000mb(is MB correct?) to work with and I can have as many of the Objects in the cache as I want up to 1000mb before it starts kicking out the LRU?

1
It means the cache will never exceed 1,000 entries. Due to the internal design, it may evict prior to that threshold. To evict based on the the object size (weight) use a Weigher and maximumWeight. While not recommended, you can use jamm to estimate the runtime size. - Ben Manes
You might wanna go through Google's Guava wiki on how they evict the cache entries. Specifically for your question, look at Size-based eviction. I think this line is interesting - Warning: the cache may evict entries before this limit is exceeded -- typically when the cache size is approaching the limit. - afrin216
There is no limit to the sizes of the objects. (Even trying to measure the size of Java objects in bytes is an expensive, confusing, platform-dependent, and error-prone endeavour.) - Louis Wasserman

1 Answers

6
votes

The maximumSize() method refers to the (approximate) maximum number of entries in the cache. It makes no guarantees about how much memory the cache's contents will consume (and as Louis points out, there's no reasonable way for any Java object to do so).

If you have a way of measuring the relative "cost" of caching a particular object you can specify a maximumWeight() instead. For example, if you were caching byte[] instances you could use their length as the weight and this would roughly reflect their memory footprint. For other types you'd need to determine an appropriate proxy notion of "cost".