This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
a. You can directly free the memory allocated by an object. b. You can directly run the garbage collector whenever you want to and it will be schedule in due time.. c. The garbage collector informs your object when it is about to be garbage collected. d. The garbage collector reclaims an object�s memory as soon as it becomes a candidate for garbage collection. e. The garbage collector runs in low-memory situations. I think e is correct. The answer is e b and c. Could anybody explain b and c to me. Thanks.
The answer b also appaears wrong to me. But I also doubt e. C is true because the garbage collector informs the object and you can check this by having a finalize method which will be run when the object is to be garbaged collected. B appears wrong to me because by calling System.gc() there is no gaurantee that the GC would run then or even would be able to run afterwards(Scheduling). The answer E is generally what may happen but as GC is unpredictable and may be differently implemented by different vendors so you just can't be sure. As for explaining the options : I guess that in option B by scheduling they mean a call to System.gc() which would either run GC then or later. The above para explains what C means. Where did you find the question? [ May 23, 2003: Message edited by: Anupam Sinha ]
Howdy, I mostly agree with Anupam's assessment of this question. This one's tough, because the wording of the answers given are not precise enough; the ones on the real exam are more careful. For example, even 'C' is not necessarily correct as worded -- because it is not always true! What you need to know for the exam is that an object will *not* always be informed when it is about to be collected. While finalize() *is* guaranteed to be called before an object is collected, it will be called only ONCE -- the *first* time the object is about to be collected. But if the object is resurrected *within* the finalize method (by, for example, passing a reference to that about-to-be-collected object out to some other live object), and if the object then LATER becomes eligible for gc again, next time around the garbage collector will NOT invoke finalize(). In other words, the garbage collector will invoke finalize() only ONCE in an object's life, regardless of how many times it becomes eligible. So it is entirely possible that an object becomes eligible, and the garbage collector checks a list to see, "Ah-ha, I see that I have already called finalize() on this one... so this time I'll skip finalize() and just reclaim the memory." And in that case, the object would NOT be informed even though it is about to be collected, and so answer 'C' would be incorrect. I actually do not believe *any* of these answers are correct as written. They would need to be worded more precisely to make them valid. cheers, -Kathy