Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 598
dermoritz Avatar asked Jan 30 '26 14:01

dermoritz


1 Answers

Actually, free memory isn't all that great of a metric for cache eviction. The reason is because of garbage collection. Running out of free memory may just mean that it is now time for the garbage collector to run, after which you'll suddenly have lots of free memory. So you don't want to drop stuff from the cache just because you have a lot of accumulated garbage.

One option is to use softValues(), but I would strongly recommend against that, as soft references can really hurt production performance.

The right thing to do is to carefully select a maximumSize which in essence bounds the total amount of memory your cache will consume. If entries take up variable amounts of space then you can use maximumWeight instead to model that.

like image 129
fry Avatar answered Feb 01 '26 04:02

fry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!