This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: Top 15 Garbage Collection Interview Questions
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
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.
2.How JVM can destroy unreferenced object?
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.
3.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.
4.Can we force garbage collector?
No, we can not force garbage collector to destroy objects , but we can request it.
5.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();
6.What is the algorithm JVM internally uses for destroying objects?
"mark and swap" is the algorithm JVM internally uses.
7.Which part of the memory is involved in Garbage Collection?
Heap.
8.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.
9. When does an object become eligible for garbage collection?
An object becomes eligible for garbage collection when no live thread can access it.
10. 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.
// 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
// 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
}
}
11.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.
12.How many times does the garbage collector calls the finalize() method for an object?
Only once.
13.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
14.What are the different ways to call garbage collector?
System.gc();
Runtime.getRuntime().gc();
15. 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.