I have little bit confusion about GC, when GC performs it stops all threads and collects Garbage references.
after GC , does jvm perform any compaction ?
Compaction: means moving "in-use" memory areas to eliminate holes caused by terminated references
if yes then does jvm maintain the old references or it assigns new reference to all remaining objects?
When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.
Java uses a technique known as garbage collection to remove objects that are no longer needed. The garbage collector is Java's grim reaper. It lingers in the background, stalking objects and awaiting their demise. It finds and watches them, periodically counting references to them to see when their time has come.
Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.
By reassigning the reference variable to some other object We can make the reference variable to refer to another object. Decouple the reference variable from the object and set it to refer to another object, so the object which was referring to before reassigning is eligible for GC.
About compaction: it depends on the algorithm used. Since all young-generation GCs are copying collectors: yes, they do campaction, they even relocate all surviving objects to a different memory-area. With old-generation collectors it depends on what collection-algorithm is used. While the standard mark-sweep-compact-collector is compacting (well, that's why the "compact" is there) the CMS-collector (concurrent mark sweep) usually does not do any compacting unless it is unavoidable and thus has to manage heap-fragmentation.
And when objects are moved (whether because of using a copy-collector or because of compaction) the existing references are updated to point to the new memory-location.
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