Home > OS >  Guava cache maximum number of elements if maximumSize is not set
Guava cache maximum number of elements if maximumSize is not set

Time:02-04

I'm trying to figure out what the number of elements the cache can hold if I do not specify any maximum size.

Is there any upper-bound to how many entries can be contained by cacheWithoutSize ?

Cache<String, Object> cacheWithoutSize = CacheBuilder.newBuilder() .build();

I read through the release doc, could not find any answer : https://guava.dev/releases/18.0/api/docs/com/google/common/cache/CacheBuilder.html#:~:text=maximumSize(10000) .

CodePudding user response:

Extending what Louis Wasserman said...

Let's assume the absolute limit on cache size (entry count) is Integer.MAX_VALUE, which is a bit over 2 billion. In realistic caching scenarios you're very likely to run out of heap memory long before you get anywhere near the theoretical maximum cache size.

For example, if we assume Long keys mapping to String values that each use 20 bytes, that's 24 bytes per entry exclusive of overhead, so a maximal cache would contain roughly 50 GiB of data. Few machines can support a 50 GiB heap allocation. And remember that overhead (e.g., the cache data structure itself) would make the real memory usage even larger.

  • Related