memory for cache while using maximumSize #846
-
Hi , would adding the weigher and providing a memory value , make sure that the allotted memory is within the maximumWeight? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The cache uses heap memory by layering on top of As you said, a weigher lets you calculate the cost of the key/value pair. That's actually quite tricky on the JVM (see JEP-8249196) so we don't provide an explicit strategy. You can use java object layout, jamm, etc for a rough estimate. Typically to understand the tradeoff of how large the cache should be requires analyzing the hit rate curve to decide when you reach a sweet spot. This is usually only if the cache is critical, else a rough guess is made and exported to stats in case something later goes awry. To analyze you can simply log the hash of keys when accessing and run that through the simulator for different cache sizes. Unfortunately most attempts that I have seen to automatically tune cache sizes fail, so layered caches (e.g. caffeine + memcached) is usually a nice pairing to find a good balance. This is especially true because caching can break the generational hypothesis so large caches might incur higher GC overhead, so some will use off-heap values with Caffeine's on heap for policy management. It all depends on the application, infrastructure, ect that makes the most sense as this library is not opinionated in what you consider the best way to use it. |
Beta Was this translation helpful? Give feedback.
The cache uses heap memory by layering on top of
ConcurrentHashMap
. It wraps the value to capture additional per-entry metadata, like the expiration timestamp, and holds a few per cache instance data structures (like the frequency histogram). The metadata overhead is fairly inexpensive and a rough evaluation is captured on the wiki.As you said, a weigher lets you calculate the cost of the key/value pair. That's actually quite tricky on the JVM (see JEP-8249196) so we don't provide an explicit strategy. You can use java object layout, jamm, etc for a rough estimate.
Typically to understand the tradeoff of how large the cache should be requires analyzing the hit rate curve to decide when y…