Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does object reference change after GC

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?

like image 241
Greesh Kumar Avatar asked Feb 22 '16 07:02

Greesh Kumar


People also ask

How does garbage collector know which objects to free?

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.

How does Java know that an object is no longer needed?

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.

What are the phases of garbage collection?

Garbage collection then goes through the three phases: mark, sweep, and, if required, compaction.

How can we make an object eligible for GC?

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.


1 Answers

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.

like image 156
piet.t Avatar answered Oct 18 '22 03:10

piet.t