How many ways can we make an object eligible for GC?

How many ways we can make an object eligible for GC?

How many ways can we make an object eligible for GC?

How many ways can we make an object eligible for GC?
  • Even through programmer is not responsible to destroy useless objects it is highly recommended to make an object eligible for GC. If it no longer a requirement.
  • The garbage collector will look for objects which aren’t being used anymore and gets rid of them, freeing up the memory so other new objects can use that piece of memory.
  • When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.
  • An object is said to be eligible for GC if and only if it doesn’t contain any reference variable.
  • The following are various ways to make an object eligible for GC
    1. Nullifying the reference variable
    2. Reassigning the reference variable
    3.  The object created inside a method
    4. The Island of Isolation.

Nullifying the reference variable

  • If an object no longer required then assign null to all its reference variables then that object automatically eligible for Garbage collection.
  • This approach is nothing but nullifying the reference variable.
  • Employee e1=new Employee();
    Employee e1=new Employee();
    ..
    ..(No object is eligible for GC)
    ..
    s1=null;  //s1 is eligible for GC
    s2=null;  //two object is eligible for GC
    

 

Reassigning the reference variable

  • If an object no longer required then reassign its reference variable to some other object then old object by-default eligible for garbage collection.
  • Employee e1=new Employee();
    Employee e2=new Employee();
    ...
    ...(No object is eligible for GC)
    ...
    e1=new Employee(); // One object is eligible for GC
    e2=e1;             // two object is eligibe for GC
    

 

Object created inside the method

  • The objects which are created inside a method are by-default eligible for GC once method complete.
  • class Test
    {
     public static void main(String args[])
      {
        m1();
    ...    // two obejct is eligible for GC
      }
     public static void m1()
      {
        Test t1=new Test();
        Test t2=new Test();
      }
    }

 

Island of isolation

  • If an object doesn’t contain any reference variable then it is eligible for Garbage collection always
  • Even though the object having references sometimes it is eligible for Garbage collection(If all references are internal reference ex. Island of Isolation)
  • class test
    {
      int i;
      public static void main(String args[])
      {
        Test t1=new Test();
        Test t2=new Test();
        Test t3=new Test();
        t1.i=t2;
        t2.i=t3;
        t3.i=t1;
        ...(no object is eligible for GC)
        t1=null;
        ...(no object is eligible for GC)
        t2=null;
        ...(no object is eligible for GC)
        t3=null;
        ...(3 object is eligible for GC)
      }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*