This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Select two guaranteed behaviour of GC and Finalization Mechanism. 1. An object is deleted as soon as there are no more references that denote the object. 2. The finalize() method will eventually be called on every object 3. The finalize() method will never be called more than once on an object 4. An object will not be garbage collected as long as it is possible for a live thread to access it through a reference 5. The garbage collector will use a mark and sweep algorithm hi, i think , the answers 1,2,3,4 are correct.But they said only 2 are correct answers ..how ?
[ January 15, 2004: Message edited by: Mohamed Yasin ]
Hi Mohamed. Different vendors may implement garbage collection differently so there's no telling how soon an object will immediately be deleted when there are no more references that denote the object. Also there are different algorithms for a garbage collection not only mark-sweep. What is guaranteed however is that the finalize() method will eventually be called on an object before it is garbage collected but only happens once. So if you've coded, for example, logic in the finalize method that makes the object uneligible for garbage collection, the next time GC runs and if the object is eligible for garbage collection it will not call finalize() the second time around. So I think nos. 2, 3 are the right answers.
Jim, Answer 2 is not right. Objects that are still referenced when the application ends are not garbage collected. Also, it is possible that objects that are eligible for garbage collection are not collected because the application ended. I believe the correct answers are 3 and 4 although I don't like the wording of 4. The phrase "...as long as it is possible for a live thread to access it through a reference" is awkward at best. Changing this to, "...as long as a live thread has access to it through a reference" would improve things.
Joined: Mar 07, 2003
Don, No. 2 has the words "will eventually be called on every object". So I'm assuming that event will come when it has no more references that points to it. I dont like the wording on 4 either so i'm not electing it to be the right answer.
Joined: Dec 05, 2003
2. The finalize() method will eventually be called on every object
I guess we just read 2 differently. I don't see how we can correctly say that every object will have its finalize method called when some objects are still referenced when the application ends. It seems to me that the term "every object" is all inclusive so I can't see that every object will have its finalize method called.
So I'm assuming that event will come when it has no more references that points to it.
But what if there is no time when some object "has no more references that point to it"? PS It seems I addressed my first response some mysterious cowpoke named Jim who has not weighed in on this question. It was dinner time so I guess I was weak from lack of grub. Beans, again.
Joined: Mar 07, 2003
Good point Don. Yeah, I was wondering who Jim was.