==== 37. Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object. b) The finalize method is always called before an object is garbage collected. c) Any class that includes a finalize method should invoke its superclass' finalize method. d) Garbage collection behavior is very predictable ===
The answer given was a,b and c. However I find that 'a' is the only answer.
'b' is false because, if finalize is called once and the object becomes ineligible for GC at this time then, when the object is actually garbage collected the next time, finalize is not called. choice 'b' uses "ALWAYS" as a qualifier in its answer and becomes false thus.
'c' is false, because it is legal even if we dont invoke the super class' finalize method in sub class' finalize().
Am I right? Please clarify me on the same.
Btw. Is it always good to call super.finalize() in sub class' finalize method??? Please pass your comments on this. Thanks!
a. is false - Reason, you can not invoke GC. You can only suggest . b. is correct c. is false, as any super class can be called independently. So, if superclass is running and subclass calls 'finalize' method of superclass - you can imagine what would happen d. false. It is unpredictable
What i can see is only b is correct. Please correct if I am wrong.
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Joined: Sep 15, 2004
As per Choice 'a' , What "INVOKE" means is Runtime.getRuntime().gc(), where we invoke Garbage Collector. Whether our call is serviced or not, is left to JVM. So Choice 'a' is right!
See my previous post on the same thread to know, why I feel, Choice 'b' is false.
Others, can you please convince me and Nitin on the answers we have given? Thanks!
a. Manikandan is correct. The intention of this question is only to test that you know about the System.gc() and Runtime.getRuntime.gc() methods, not whether the garbage collection will actually take place. The wording of the question is rather shaky.
b. correct, if the object is swept up by the garbage collector then the finalize method will be called. Whether or not the finalize method "resurrects" the object is irrelevant, the finalize method will be called by the garbage collector.
c. correct, if you write a finalize method you should call super.finalize() within your method. This is to ensure that the complete object is finalized, not just part of it (remember overriding ?).
d. false, as we all know by now, garbage collection is supposed to be run in a low priority daemon thread snd you cannot predict if, or when, such a thread will run. Also garbage collection algorithms vary from one Java version to another (on some I believe you can select the algorithm to be used on the call to the java runtime when you start the program).