Java Garbage Collections Interview Questions

Q1) Which part of the memory is involved in Garbage Collection? Stack or Heap?

Ans) Heap

Q2)What is responsiblity of Garbage Collector?

Ans) Garbage collector frees the memory occupied by the unreachable objects during the java program by deleting these unreachable objects.
It ensures that the available memory will be used efficiently, but does not guarantee that there will be sufficient memory for the program to run.

Q3) Is garbage collector a daemon thread?

Ans) Yes GC is a daemon thread. A daemon thread runs behind the application. It is started by JVM. The thread stops when all non-daemon threads stop.

Q4)How is Garbage Collection managed?

Ans)The JVM controls the Garbage Collector; it decides when to run the Garbage Collector. JVM runs the Garbage Collector when it realizes that the memory is running low. The behavior of GC can be tuned by passing parameters to JVM. One can request the Garbage Collection to happen from within the java program but there is no guarantee that this request will be taken care of by jvm.

Q5) When does an object become eligible for garbage collection?

Ans) An object becomes eligible for Garbage Collection when no live thread can access it.

Q6) What are the different ways to make an object eligible for Garbage Collection when it is no longer needed?

Ans)

  • Set all available object references to null once the purpose of creating the object is served :
    public class GarbageCollnTest1 {
       public static void main (String [] args){
          String str = "Set the object ref to null";
            //String object referenced by variable str is not eligible for GC yet
            str = null;
        /*String object referenced by variable str becomes eligible for GC */
        }
      }
  • Make the reference variable to refer to another object : Decouple the reference variable from the object and set it refer to another object, so the object which it was referring to before reassigning is eligible for Garbage Collection.

    publc class GarbageCollnTest2 {
      public static void main(String [] args){
      String str1 = "Garbage collected after use";
      String str2 = "Another String";
      System.out.println(str1);
      //String object referred by str1 is not eligible for GC yet
      str1 = str2;
      /* Now the str1 variable referes to the String object "Another String" and the object "Garbage collected after use" is not referred by any variable and hence is eligible for GC */
      }
    }
  • Creating Islands of Isolation : If you have two instance reference variables which are referring to the instances of the same class, and these two reference variables refer to each other and the objects referred by these reference variables do not have any other valid reference then these two objects are said to form an Island of Isolation and are eligible for Garbage Collection.

    public class GCTest3 {
        GCTest3 g;    
       public static void main(String [] str){
            GCTest3 gc1 = new GCTest3();
            GCTest3 gc2 = new GCTest3();
            gc1.g = gc2; //gc1 refers to gc2
            gc2.g = gc1; //gc2 refers to gc1
            gc1 = null;
            gc2 = null;
            //gc1 and gc2 refer to each other and have no other valid //references
            //gc1 and gc2 form Island of Isolation
          //gc1 and gc2 are eligible for Garbage collection here 
        }
    }

Q7) Can the Garbage Collection be forced by any means?

Ans)No. The Garbage Collection can not be forced, though there are few ways by which it can be requested there is no guarantee that these requests will be taken care of by JVM.

Q8) How can the Garbage Collection be requested?

Ans) There are two ways in which we can request the jvm to execute the Garbage Collection.

  • The methods to perform the garbage collections are present in the Runtime class provided by java. The Runtime class is a Singleton for each java main program. The method getRuntime() returns a singleton instance of the Runtime class. The method gc() can be invoked using this instance of Runtime to request the garbage collection.
  • Call the System class System.gc() method which will request the jvm to perform GC.

Q9) What is the purpose of overriding finalize() method?

Ans) The finalize() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.

Q11) How many times does the garbage collector calls the finalize() method for an object?

Ans) Only once.

Q12) What happens if an uncaught exception is thrown from during the execution of the finalize() method of an object?

Ans) The exception will be ignored and the garbage collection (finalization) of that object terminates.

Q13) What are different ways to call garbage collector?

Ans) Garbage collection can be invoked using System.gc() or Runtime.getRuntime().gc().

Q14) How to enable/disable call of finalize() method of exit of the application

Ans) Runtime.getRuntime().runFinalizersOnExit(boolean value) . Passing the boolean value will either disable or enable the finalize() call.