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. |
|
Q3) Is garbage collector a dameon thread? Ans) Yes GC is a dameon thread. A dameon thread runs behind the application. It is started by JVM. The thread stops when all non-dameon threads stop. |
Q4)Garbage Collector is controlled by whom? 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, but this behavior of jvm can not be guaranteed. |
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) 1. 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";} } 2. 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"; } } 3) 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();} } |
|
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.
|
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. |
Q10) If an object becomes eligible for Garbage Collection and its finalize() method has been called and inside this method the object becomes accessible by a live thread of execution and is not garbage collected. Later at some point the same object becomes eligible for Garbage collection, will the finalize() method be called again? Ans) No |
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?
|
Q13) What are different ways to call garbage collector?
|
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. |