In this tutorial we will touch upon some of the most commonly asked Java garbage collection interview questions.
Q: Explain garbage collection and how can you force the garbage collection?
Garbage collection is the concept of identifying and deleting the objects that are no longer needed by program so that the resources can be reused by program. A java object is said to be ready for garbage collector, when it is no longer referenced. Garbage collector is a program that runs by JVM which is used for automatic memory management.
Every class inherits finalize()
method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. On calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will be garbage collected. There is no guarantee that Garbage collection will start immediately upon request of System.gc().
Q: It is a daemon thread. Can an object’s finalize() method be invoked while it is reachable?
An object’s finalize() method cannot be invoked by the garbage collector while the object is still reachable. However, an object’s finalize() method may be invoked by other objects.
Q: Does garbage collection guarantee that a program will not run out of memory?
Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.
Q: What is the purpose of finalization?
The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup, before the object gets garbage collected. For example, closing an opened database Connection.
Q: If an object is garbage collected, can it become reachable again?
Once an object is garbage collected, It can no longer become reachable again.
Q: Which part of the memory is involved in Garbage Collection? Stack or Heap?
Heap
Q: Is garbage collector a dameon thread?
Yes. A dameon thread runs behind the application. It is started by JVM.
Q: Who controls Garbage Collector?
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. 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.
Q: When does an object become eligible for garbage collection?
An object becomes eligible for Garbage Collection when no live thread can access it.
Q: Can the Garbage Collection be forced by any means?
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.
Q: How can the Garbage Collection be requested?
There are two ways in which we can request the jvm to execute the Garbage Collection.
1. Runtime.getRuntime().gc();
2. System.gc();
Q: What is the 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.
Q: How many times does the garbage collector call the finalize() method for an object?
Only once.
Q: What happens if an uncaught exception is thrown from during the execution of the finalize() method of an object?
The exception will be ignored and the garbage collection (finalization) of that object terminates.
Q: How to enable/disable call of finalize() method of exit of the application
Runtime.getRuntime().runFinalizersOnExit(boolean value) . Passing the boolean value will either disable or enable the finalize() call.