Which of the following statements related to Garbage Collection are correct. Select the two correct answers. a. It is possible for a program to free memory at a given time. b. Garbage Collection feature of Java ensures that the program never runs out of memory. c. It is possible for a program to make an object available for Garbage Collection. d. The finalize method of an object is invoked before garbage collection is performed on the object.
The answer given is c,d. I have a question regarding d. "The finalize method is never invoked more than once by a Java virtual machine for any given object." According to this statement option d is incomplete. Or may be I do not understand what the above statement means.. Any thoughts? Thanks.
Joined: Aug 25, 2003
Oblect class API:
The general contract of finalize is that it is invoked if and when the JavaTM virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.
Joined: May 05, 2000
You are correct, Cathy. If finalize runs but the object is not gc'ed then finalize will not run later when the object is gc'ed. But "d" is correct too. Before an object is gc'ed, finalize will run. The object can not be gc'ed unless finalize has run. It may be that finalize ran long before the object was gc'ed but the question doesn't state "immediately" before garbage collection.