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?
The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
Garbage Collection runs on the heap memory to free the memory used by objects that don't have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.
In Java, garbage collection happens automatically during the lifetime of a program. This eliminates the need to de-allocate memory and therefore avoids memory leaks. Java Garbage Collection is the process by which Java programs perform automatic memory management.
D. Objects that can be reached from a live thread will never be garbage collected. Explanation: Option D is correct.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With