300+ [LATEST] Java Garbage Collection Interview Questions and Answers

1. What Are The Different Ways To Make An Object Eligible For Garbage Collection When It Is No Longer Needed?

  • 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

}

}

2. When Does An Object Become Eligible For Garbage Collection?

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

3. How Many Times Does The Garbage Collector Calls The Finalize() Method For An Object?

Only once.

4. Can We Force Garbage Collector?

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

5. How To Enable /disable Call Of Finalize() Method Of Exit Of Application?

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

6. What Is Purpose Of Overriding Finalize() Method?

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.

7. So Can You Guarantee Objects Destruction?

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

8. How Can We Request Jvm To Start Garbage Collection Process?

  • 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();

9. How Jvm Can Destroy Unreferenced Object?

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

10. Which Part Of The Memory Is Involved In Garbage Collection?

Heap.

11. What Are The Different Ways To Call Garbage Collector?

  • System.gc();
  • Runtime.getRuntime().gc();

12. What Is The Algorithm Jvm Internally Uses For Destroying Objects?

“mark and swap” is the algorithm JVM internally uses.

13. What Is Garbage Collection In Java?

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

14. What Is Responsibility Of Garbage Collector?

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

15. What Happens If An Uncaught Exception Is Thrown From During The Execution Of Finalize() Method Of An Object?

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