Skip to content

Memory overhead

Ben Manes edited this page Mar 23, 2015 · 18 revisions

The cache and entry objects only require certain fields depending on the configuration options specified when creating the cache. To minimize memory, code generation is used to avoid retaining unnecessary fields.

Estimates were performed using Java Agent for Memory Measurements to calculate the runtime size. The size may be affected by compressed references, object padding, etc. applied by the JVM. This benchmark may be run using gradle memoryOverhead which executes MemoryBenchmark.

By forking Java 5's ConcurrentHashMap, Guava has a slight advantage with per-entry optimizations. This is evident when weak and soft reference caching is used. Caffeine must retain additional fields to remove a collected entry due to decorating a Java 8 ConcurrentHashMap.

Guava uses an unbounded ConcurrentLinkedQueue per segment. This reduces the baseline footprint at the cost of unpredictable growth, GC pauses, and slower execution. Caffeine pre-allocates a segmented ring buffer resulting in a higher, but fixed, initial footprint.

Pending optimizations

Caffeine's ring buffers are fixed to a size optimal for high concurrency. Instead, the buffers should grow dynamically based upon contention so that footprint is minimized.

Unbounded

Cache Baseline Entry
Caffeine 656 bytes 42 bytes
Guava 1,136 bytes 58 bytes

Maximum Size

Cache Baseline Entry
Caffeine 23,552 bytes 74 bytes
Guava 1,560 bytes 74 bytes

Maximum Size & Expire after Access

Cache Baseline Entry
Caffeine 23,640 bytes 82 bytes
Guava 1,560 bytes 74 bytes

Maximum Size & Expire after Write

Cache Baseline Entry
Caffeine 23,672 bytes 90 bytes
Guava 1,728 bytes 90 bytes

Maximum Size & Refresh after Write

Cache Baseline Entry
Caffeine 23,664 bytes 82 bytes
Guava 27,480 bytes 90 bytes

Maximum Weight

Cache Baseline Entry
Caffeine 23,584 bytes 74 bytes
Guava 1,552 bytes 74 bytes

Expire after Access

Cache Baseline Entry
Caffeine 23,616 bytes 82 bytes
Guava 1,560 bytes 74 bytes

Expire after Write

Cache Baseline Entry
Caffeine 504 bytes 82 bytes
Guava 1,360 bytes 74 bytes

Expire after Access & after Write

Cache Baseline Entry
Caffeine 23,664 bytes 98 bytes
Guava 1,728 bytes 90 bytes

Weak Keys

Cache Baseline Entry
Caffeine 384 bytes 98 bytes
Guava 1,416 bytes 66 bytes

Weak Values

Cache Baseline Entry
Caffeine 384 bytes 98 bytes
Guava 1,416 bytes 74 bytes

Weak Keys & Weak Values

Cache Baseline Entry
Caffeine 440 bytes 130 bytes
Guava 1,512 bytes 82 bytes

Weak Keys & Soft Values

Cache Baseline Entry
Caffeine 440 bytes 146 bytes
Guava 1,584 bytes 90 bytes

Soft Values

Cache Baseline Entry
Caffeine 384 bytes 114 bytes
Guava 1,416 bytes 90 bytes
Clone this wiki locally