Can Java garbage collector randomly delete objects in the On-Heap tier?
Asked Answered
M

2

6

Ehcache's documentation states that the Heap tier is subject to Java garbage collection (as opposite to the Off-heap tier & Disk store).

Now, does this mean that objects in the Heap tier can be spontaneously deleted by GC? Obviously, they are deleted by Ehcache when they expire or when it runs out of space - which is a well-defined behaviour. But on top of that, can GC just come and randomly kill some objects just like that, without even moving them to a lower tier?

Misgiving answered 2/11, 2018 at 10:19 Comment(0)
R
5

GC won't collect a live object, that is an object which is reachable from a live thread. Objects in the on-heap Ehcache storage are reachable so they won't be collected.

Ehcache used to experiment with WeakReference but according to this post this idea was abandoned:

I thought this was a cool idea. In production our caches ended up looking like Swiss cheese as elements randomly disappeared. I was hoping the VM would keep all elements and only start discarding before running out of memory. Not so. It was removed about 8 months ago although I noted a few references in the java doc today.

Rank answered 2/11, 2018 at 10:41 Comment(0)
C
0

No. GC will never remove any object from the cache. Ehcache doesn't use any weak or soft reference.

The sentence you mention is just stating that entries cached on-heap are on-heap. So they will be maintained by the GC. So if you have a big cache, you have a big heap and you need to tune your GC accordingly to prevent it from slowing you down. Also, cache entries tend to go in the old generation. So you might need an efficient old generation GC.

Off-heap storage won't have that problem at all. However, adding and retrieving entries will be a bit slower since they need to be serialized.

Clubwoman answered 26/11, 2018 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.