Garbage Collection: How it’s done

And a Garbage Collector also guarantees that any live Object that has an active reference will not get removed from the memory.Reference Counted Garbage CollectionThe reference count garbage collection keeps a track of the number of references for a particular object in the memory..Let’s look at the following code segment.Object a = new Object(); // Reference Count(OB1) starts at 1Object b = a; // Reference Count (OB1) incremented to 2 as a new reference is addedObject c = new Object();b.someMethod();b = null; // Reference Count(OB1) decremented to 1 as reference goes awaya = c; // Reference Count(OB1) decremented to 0When executing the line Object a = new Object(), a new object (let’s say OB1 ) is created in the memory and the reference count (for OB1) starts at 1.When the reference for the OB1 in the variable “a” is copied to “b”, the reference counter increases by one as now two variables have the reference for OB1.When “b” is assigned to null, there reference for OB1 decreases leaving only the variable “a” having a reference for OB1.When the value of “a” is updated by the value of “c” (which is having a reference for a whole new object), there reference counter for the OB1 becomes zero leaving the OB1 available for garbage collection.Drawbacks in Reference Counted GCThe main disadvantage in the reference counted garbage collection is its inability to identify circular references..To understand the circular references, let’s have a look into the below code segment.Consider two classes A and B having each other’s references.class A { private B b; public void setB(B b) { this.b = b; }}class B { private A a; public void setA(A a) { this.a = a; }}Now in the main method, we can create new objects for both of these classes and assign the references.public class Main { public static void main(String[] args) { A one = new A(); B two = new B(); // Make the objects refer to each other (creates a circular reference) one.setB(two); two.setA(one); // Throw away the references from the main method; the two objects are // still referring to each other one = null; two = null; }}When we assign null values for the two variables one and two, the external references existed with the class objects (“A” and “B”) created at the beginning will be removed..Still, they won’t be eligible for garbage collection as the reference counters of those two objects will not become zero due to object “A” having its reference inside “B” and the object “B” having its reference inside “A”.Mark and Sweep Garbage CollectionAs the name suggests, Mark and Sweep garbage collectors have two phases1..Mark Phase2..Sweep PhaseMark PhaseDuring the Mark phase, the GC identifies the objects that are still in use and set their “mark bit” to true..The search starts with a root set of references kept in local variables in the stack or global variables..Starting from the root references the GC will conduct a depth search for the objects that have reachable references from the root..Any object that keeps a reference of another object, keeps that object alive.It is important to keep note that during the Mark phase, the application threads are stopped to avoid the changes that can happen to the object state during the marking phase.Source: cyclic references are not an issue for a Mark and Sweep GC..If you observe the above diagram, a cyclic reference exists (shown by the square) but it is unreachable from the root..Hence, those types of references will not be marked as live allowing GC to collect as garbage.Sweep PhaseIn the sweep face, all the unmarked objects from the Mark phase will be removed from the memory freeing up space.Source: you can observe from the above diagram, there may exist plenty of free regions after the sweep phase..But, due to this fragmentation, the next memory allocation may fail if it is bigger than all the existing free regions.To overcome this problem, an additional Compact phase was added.Mark-Sweep-Compact Garbage CollectionAfter the sweep phase, all the memory locations are rearranged to provide a more compact memory allocation.. More details

Leave a Reply