Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

ehcache

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?

like image 310
Leszek Pachura Avatar asked Nov 02 '18 10:11

Leszek Pachura


People also ask

Does garbage collection happen in heap?

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.

Does garbage collector work on stack or heap memory?

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.

What is the rule of garbage collector in Java?

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.

Which of the following statements about garbage collection in Java are true?

D. Objects that can be reached from a live thread will never be garbage collected. Explanation: Option D is correct.


1 Answers

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.

like image 166
Karol Dowbecki Avatar answered Oct 08 '22 05:10

Karol Dowbecki