I am confused with the answers given to the two questions, in two different mock exam papers.
The questions are as follows:
I. Which of the following statements are true? 1. The automatic garbage collection of the JVM prevents programs from ever running out of memory 2. A program can suggest that garbage collection be perfomed but not force it. 3. Garbage collection is platform indpendent. 4. An object becomes eligible for garbage collction when all references denoting it are set to null.
Answer to this question are 2, 4.
II. You are concerned that your program may attempt to use more memory than is available . To avoid this situation you want to ensure that the JVM will run its garbage collection just before you start a complex routine. what can you do to be certain that garbage collection will run when you want.
1. You cannot be certain when garbage collection will run 2. use the Runtime.ge() method to force garbage collection. 3. Ensure that all variables you require to be garbage collected are set to null. 4. Use the System.gc() method to force garbage collection.
Answer to this question is 2.
Can any one explain me why only 2nd answer is right in question II. Thanks in advance.
Well for the 1st Q . I think the statement "An object becomes eligible for garbage collction when all references denoting it are set to null." is a bit ambiguous. If u had read k&B then u had gone through the islands of Isolation case where the references to the objects still exist. for the 2nd Q, i feel that alternate 1 is correct i.e. You cannot be certain when garbage collection will run. As we can only request and can't force Garbage Collection.
Mohit Agarwal. Would be SCJP. "The will to win is worthless if u do not have the will to prepare"
...you can suggest or encourage the JVM to perform garbage collection but you can not force it.
Let me re-state that point, you cannot force garbage collection, just suggest it. ... You can suggest garbage collection with System.gc(), but this does not guarantee when it will happen
In addition some mock exams talk about calling the garbage collector. System.gc() does indeed call the garbage collection routine, and it gets called immediately and after the call, control is returned to the program. However this does not imply that any objects are guaranteed to be garbage collected.
According to this explanation, the first #2 is true. The second #2 is false. I think everyone would agree that the second #2 is false.
1) You cannot be certain when garbage collection will run
Although there is a Runtime.gc(), this only suggests that the Java Virtual Machine does its garbage collection. You can never be certain when the garbage collector will run. Roberts and Heller is more specific abou this than Boone. This uncertainty can cause consternation for C++ programmers who wish to run finalize methods with the same intent as they use destructor methods.
[ October 04, 2004: Message edited by: Louie van Bommel ]
yes, the correct answer to the 2nd question is 1, nor 2.
Either the author of that mock exam did a poor job editing and proofreading his answers or he didn't understand the garbage collection system.
Problem is that the javadoc for Runtime.gc() is somewhat ambiguous. If you read JUST the short description in the function list it will indeed hint that it forces the garbage collector to run. The long description though makes it clear this is not the case and all it does is request garbage collection is run. And even IF the garbage collector runs that will never ensure enough free memory. It might make things even worse and cause the JVM to crash if there's not enough memory for the garbage collector to run in the first place (though JVMs are supposed to guard against this and attempt to run the garbage collector before memory starvation reaches that point, thus making sure there's always memory to run the garbage collector at least).
Runs the garbage collector.
Runs the garbage collector. Calling this method suggests that the Java virtual machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the virtual machine has made its best effort to recycle all discarded objects.
Also, System.gc() doesn't tell AT ALL that the call does not force the garbage collector to run but only requests it:
Runs the garbage collector. Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
is it correct in one of the mock exams the first one is correct or
-->Ensure that all variables you require to be garbage collected are set to null.
Variables are not garbage collected, only the 'objects' are garbage collected. I think that's the reason the answer in the mock exam seems to be wrong.Coming to the other question, yes when all the references of an object are set to null then that object becomes eligible for garbage collection.
I hope it helps
Impossible is I M Possible
Louie van Bommel
Joined: Aug 17, 2004
As prashant pointed out, the wording seems
Yup, setting ALL (keyword is ALL) references to an object make it eligible.
Two points: 1. If you ensured that ALL reference variables that referenced an object ObjectX were set to null, the object would be eligible for garbage collection.
2. If you set a reference variable x to null, the object it had referenced is not necessarily eligible for garbage collection.
Try this at home [pseudocode alert]
Note here that I set the reference variable x to null, yet the object is not garbage collected.
Setting a reference variable (x) to null doesn't necessarily cause an object (XObject) to be eligible for garbage collection. There could still be other references (variable y) pointing to the object (XObject.
If you were to find all references to the object XObject, and set them ALL to null, the object will be eligible for garbage collection.
And yes, I agree the reason for it not being an acceptable answer is because it says "variables" to be garbage collected.
You do know that Marcus has a much better exam online don't you? Those 3 exams are kinda old and intended for the 1.2 exam. Try username: guest password: password at http://www.jchq.net/phezam
[ October 05, 2004: Message edited by: Louie van Bommel ]
("variable a" -> "variable x" - Barry) [ October 06, 2004: Message edited by: Barry Gaunt ]