In Languages like C, C++ it is the responsibility of the developer to allocate/deallocate the memory needed for a program using malloc and calloc commands.
In Java, it is done by Java virtual machine automatically using the Garbage collector. In general, any object on the heap which cannot be referenced from the stack is eligible for garbage collection.
Garbage Collection uses two strategies to clear the heap.
In Java, it is done by Java virtual machine automatically using the Garbage collector. In general, any object on the heap which cannot be referenced from the stack is eligible for garbage collection.
Garbage Collection uses two strategies to clear the heap.
- Match and sweep - Instead of identifying the objects with no references, GC maps all the objects for which there is a reference from the stack and sweeps others.
- Generation Collection - It was discussed as part of Java Memory management crash course where the heap is divided into four sections. Objects from the young generation are swept more than the objects in the old generation.