Size-based eviction
Limit the cache by maximum number of entries or total weight.Maximum size
Evict entries when the cache exceeds a maximum number:When
maximumSize is 0, entries are evicted immediately after loading. This is useful for testing or temporarily disabling caching.Maximum weight
Evict entries based on cumulative weight using a customWeigher:
Custom weigher
ImplementWeigher for complex weight calculations:
Weight must be non-negative. Weights are measured when entries are inserted or updated and remain static during the entry’s lifetime.
Eviction policy
Caffeine uses the Window TinyLFU eviction policy, which combines recency and frequency to maximize hit rate.How it works
- Window
- Main space
- Frequency sketch
New entries enter a probationary space (the “window”) where they are evaluated based on recent access patterns. This protects against cache pollution from one-hit-wonders.
Accessing eviction policy
Inspect and control eviction behavior:Eviction order inspection
View entries ordered by likelihood of eviction:Reference-based eviction
Automatically evict entries when keys or values are garbage collected.Weak keys
Keys wrapped in weak references are evicted when garbage collected:With
weakKeys(), the cache uses identity (==) comparison for keys instead of equals(), similar to IdentityHashMap.Weak values
Values wrapped in weak references are evicted when garbage collected:Soft values
Values wrapped in soft references are garbage collected in response to memory pressure:In most cases, it’s better to use
maximumSize() instead of soft references. Only use soft references if you understand their performance implications.Eviction timing
Understand when eviction maintenance occurs:Automatic maintenance
Eviction is performed opportunistically:- During write operations (most common)
- During read operations when writes are absent
- Via calls to
cleanUp()
Batch operations
Eviction is batched for efficiency:Best practices
Choose appropriate size bounds
Choose appropriate size bounds
- Set
maximumSizebased on expected working set size - Use
maximumWeightwhen entries have significantly different sizes - Monitor cache statistics to tune the maximum
- Remember that the cache may temporarily exceed the maximum during eviction
Implement efficient weighers
Implement efficient weighers
- Return 0 weight for entries that should not count toward eviction
- Keep weight calculations fast (they run on every insert/update)
- Ensure weights are non-negative
- Consider both key and value sizes if memory is constrained
Reference-based eviction considerations
Reference-based eviction considerations
- Prefer
maximumSizeover reference-based eviction when possible - Use
weakKeys()for canonicalization or interning use cases - Use
softValues()only if you need memory-sensitive caching - Avoid
weakValues()unless you have specific requirements
Runtime adjustments
Runtime adjustments
- Adjust
maximumSizeormaximumWeightat runtime via the policy API - Call
cleanUp()if you need prompt eviction after size changes - Monitor
evictionCountin statistics to understand churn
Related topics
- Expiration - Remove entries based on time
- Removal - Handle eviction notifications
- Statistics - Monitor eviction metrics