Following is a question from Barry Boone's exam : Which statements about garbage collection are true? Select all valid answers.
a.You can directly free the memory allocated by an object. b.You can directly run the garbage collector whenever you want to. 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
The correct answer is b,c,e. But i am not clear about the statement c. Can someone explain it to me.
I agree with you Muhammad, I think we are faced with a poorly worded answer again... b would be correct if the answer would be "You can directly INVOKE the garbage collector whenever you want to." Though no warranty is given whether the gc will be run directly or not ! Val
Thanks Val, but ur response creates a confusion in my mind, when we say invoke, doesn't it mean run. I think all we can do is to try to run or try to invoke, all we can't do is to force to do it. --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Invoke means actually call a method upon an instance of an object, like this String s = "String1"; s.trim(); here you invoke the method trim upon instance s. Concerning the gc, you invoke gc() upon the class System. What System.gc() does is invoking gc() on an instance of class Runtime (Runtime.getRuntime().gc()). Note that the method gc in class Runtime is native. It is up to the method gc() to actually try to make the garbage collector run, but nothing is guaranteed. It is not because you "invoke" a method that the body of that method will be sequentially executed. Another example is the method start() of the class Thread. When you "invoke" the method start() on an instance of a Thread, the method returns almost immediately even if the Thread instance is not started yet. All this to say that "invoking" the method gc() does not mean "running" the garbage collector, but just that "you'd like the garbage collector to run now". let me know Muhammad if you are still confused ! Val
1. You can call the garbage collector whenever you want to. (True/False ) 2. You can directly run the garbage collector whenever you want to. (True/False)
According to me : 1. This statement is more like the statement "Garbage collection can be forced" which should be false. 2. Is this statement similar to what we mean by making a call System.gc() ? But then it should have been donot have been "invoke" instead of "run" Pls. explain..I am still not clear .