mikol
mikol

Reputation: 195

What happens when Caffeine runs out of memory?

What happens when Caffeine runs out of memory during for example put operation? Is OutOfMemoryError exception thrown, or is the operation ignored? I could not find it stated explicitly in the documentation.

Upvotes: 2

Views: 1554

Answers (1)

Ben Manes
Ben Manes

Reputation: 9591

Caffeine won't catch OutOfMemoryError in order to suppress and evict entries. When that error is thrown the JVM is left in an unpredictable state, which may not be recoverable. It is best to use -XX:+ExitOnOutOfMemoryError and -XX:+HeapDumpOnOutOfMemoryError to restart and investigate the memory leak offline.

At first it might seem beneficial to defer all of the available memory to the caches and let the garbage collector evict when needed. This is the idea behind soft references. Unfortunately, that approach has been troublesome due to making full GCs more frequent and increasing their stop-the-world pause times. We instead recommend using an explicit maximum size, where the policy is quite intelligent. If you want large caches then usually a remote tier, like memcached or redis, is a good compliment so you get the speed of local access and a lower miss penalty on cache loads.

Upvotes: 1

Related Questions