0

I’m using Hibernate 4.3.11.Final with the accompanying version of ehcache. I have a simple cache configuration which looks like the following:

<defaultCache maxElementsInMemory="10000"
     eternal="false"
     timeToIdleSeconds="86400"
     timeToLiveSeconds="86400"
     overflowToDisk="false"
     memoryStoreEvictionPolicy="LRU">
</defaultCache> 
<cache name="main" />

My question is, because the memory setting is part of the heap and the heap gets garbage collected periodically, what happens when some of the entries in my cache get garbage collected? Is it the same as those entries getting evicted from the cache?

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

Garbage collection (GC) will never collect entries from a cache to which there is a root path since they are referenced by the cache itself.

To answer the question around offheap, let's say you decide to have 500k mappings in your cache and each mapping is 10k bytes. That amounts to nearly 5GB of cached data. Data that has an impact on the GC when it runs, since it needs to perform operations around it - mark, promotions, compaction depending on GC impl. So offheap answers this problem by placing the objects outside of the area where GC happens in order to enable the application to run with a much smaller heap and thus reduced GC pauses.

All of this does not contradict that it is never the GC that will remove a cache entry. It is the opposite - once evicted, expired or replaced, then a former mapping becomes free for GC as long as there are no more root paths to it.

And this is what the explanation given in this answer says.

Community
  • 1
  • 1
Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • Sure about that? This guy begs to differ -- http://stackoverflow.com/questions/24346428/garbage-collection-when-ehcache-is-used-for-hibernate-second-level-cache . Also if they don't get collected by the GC, why did Terracotta feel the need to solve this non-existent "garbage collection problem" -- http://www.theserverside.com/news/thread.tss?thread_id=60893 . – Dave Mar 09 '16 at 14:51
  • Thanks for your clariciation. Although I did not mention off-heap, you raised it as an interesting solution to resolve the shortcomings of ehcache's onheap system. However the ehcache's offheap solution is not free, right? – Dave Mar 10 '16 at 21:41
  • right for version 2.x, but upcoming version 3 has it for free. See ehcache.org – Louis Jacomet Mar 13 '16 at 10:19