Top 15 Java Garbage Collection Interview Questions You Must Prepare 19.Mar.2024

  • Set all available object references to "null" once the purpose of creating object is served.

package com.instanceofjava;

  class GarbageCollectionTest1{

  public static void main(String [] args){

 String str="garbage collection interview questions";

// String object referenced by variable str and is not eligible for GC yet.

 str=null;

//String object referenced by variable str is 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 was referring to before reassigning is eligible for Garbage Collection

package com.instanceofjava;

 class GarbageCollectionTest2{

  public static void main(String [] args){

 String str1="garbage collection interview questions";

String str2="Top 15 garbage collection interview questions";

// String object referenced by variable str1 and str2 and is not eligible for GC yet.

 str1=str2;

//String object referenced by variable str1 is eligible for GC

 }

}

An object becomes eligible for garbage collection when no live thread can access it.

No, we can not force garbage collector to destroy objects , but we can request it.

Runtime.getRuntime().runFinalizersOnExit(boolean value). passing the boolean value  true and false will enable or disable the finalize() call.

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.

  • No, we can not guarantee objects destruction even though it is unreferenced, because we can not guarantee garbage collector execution.
  • So, we can confirm whether object is eligible for garbage collection or not.

  • We have a method called gc() in system class as static method and also in Runtime class as non static method to request JVM to start garbage collector execution.
  • System.gc();
  • Runtime.getRuntime().gc();

  • JVM internally uses a daemon thread called "garbage collector" to destroy all unreferenced objects.
  • A daemon thread is a service thread. Garbage Collector thread is called daemon thread because it provides services to JVM to destroy unreferenced objects.
  • This thread is low priority thread. Since it is a low priority thread we can not guarantee this execution.

"mark and swap" is the algorithm JVM internally uses.

  • Garbage Collection is an automatic memory management feature.
  • The process of destroying unreferenced objects is called Garbage Collection.
  • Once object is unreferenced it is considered as unused object, hence JVM automatically destroys that object.
  • In java developers responsibility is only to creating objects and unreferencing those objects after usage.

  • 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.

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