When will be an object eligible for garbage collection?


Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.

When an object created in Java program is no longer reachable or used it is eligible for garbage collection.

Following are some scenarios where a Java object could be unreachable/unused.

  • Object inside a method − In Java a method is stored in the stack memory. When you call a method, JVM fetches it into the stack and executes it. After the execution it is popped out of the stack then, all its variables will be discarded.

Therefore, when you create an object with in a method after the execution of the method this object becomes unreachable.

Example

 Live Demo

public class Sample {
   String name;
   Sample(String name){
      this.name = name;
   }
   public static void sampleMethod(){
      Sample obj1 = new Sample("myObject2");
      Sample obj = new Sample("myObject1");
   }
   protected void finalize() throws Throwable {
      System.out.println(this.name + " successfully garbage collected");
   }
   public static void main(String args[]){
      sampleMethod();
      System.gc();
   }
}

Output

myObject1 successfully garbage collected
myObject2 successfully garbage collected
  • Reassignment − When you reassign a value (another object) to an existing reference variable, the original object ends up being unreachable.

Example

 Live Demo

public class Sample {
   String name;
   Sample(String name){
      this.name = name;
   }
   protected void finalize() throws Throwable {
      System.out.println(this.name + " successfully garbage collected");
   }
   public static void main(String args[]){
      Sample obj = new Sample("myObject1");
      obj = new Sample("myObject2");
      System.gc();
   }
}

Output

myObject1 successfully garbage collected
  • Assigning null − When you assign a null value to a reference of an object it is no longer reachable.

Example

 Live Demo

public class Sample {
   String name;
   Sample(String name){
      this.name = name;
   }
   protected void finalize() throws Throwable {
      System.out.println(this.name + " successfully garbage collected");
   }
   public static void main(String args[]){
      Sample obj = new Sample("myObject1");
      obj = null;
      System.gc();
   }
}

Output

myObject1 successfully garbage collected
  • Anonymous object − Actually, the reference of the anonymous object will not be stored anywhere therefore, after the first execution you cannot call it again hence, it is unreachable.

Example

 Live Demo

public class Sample {
   String name;
   Sample(String name){
      this.name = name;
   }
   public void sampleMethod(){
      System.out.println("This is a sample method");
   }
   protected void finalize() throws Throwable {
      System.out.println(this.name + " successfully garbage collected");
   }
   public static void main(String args[]){
      new Sample("object1").sampleMethod();
      System.gc();
   }
}

Output

This is a sample method
object1 successfully garbage collected

Updated on: 29-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements