The following is a question from MindQ's mock exam. 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. I thougth only a) is correct. But the answer given is a b and c. Would anyone explain it to me. Thanks in advance.
a) The garbage collector can be invoked explicitly using a Runtime object. The garbage collector might be delayed by a higher priority thread, or run only in low memory situations. Thus the program might end before the g.c. gets a chance to run. b) The finalize method is always called before an object is garbage collected. True, if the object is garbage collected, its finalize method has been called exactly once. c) Any class that includes a finalize method should invoke its superclass' finalize method. I disagree. An instance of Object should not do so. Neither a class whose base class had not declared a finalize method. However, finalization of objects is not a chained process as construction is. That is, finalize does not automatically calls the finalize method of the base class. For a proper finalization, the last sentence in a finalize method should be "super.finalize()", as long as such method exists. It must be the last one because during the execution of a given finalize method some resources in the base class might be needed to be available. When the finalization is done, they are released via the call to super.finalize();