Ullas Kashyap
Ullas Kashyap

Reputation: 55

GuavaCache invalidateAll() vs setting the variable to null

In a java application, I'm trying to invalidate all the entries of a guava cache at the end of every hour (based on system time). To do this, I know about guavaCache's invalidateAll() method. I'm contemplating on whether to use the invalidateAll() method or set the cache variable to null and allow GC to clean up the heap. Which is better? What are the pros / cons of each approach?

My goal is to ensure that the invalidated entries are purged out of the memory as quickly as possible due to memory constraints in the system.

Upvotes: 1

Views: 147

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97331

With the approach you're describing, there's probably little benefit in using a Guava cache, and you might as well be using a HashMap.

It's likely better to correctly configure your cache's eviction strategy, e.g. by automatically removing entries that are older than one hour, or limiting the cache in size.

Read the chapter on eviction for specifics.

Upvotes: 2

Related Questions