Just passed today, but got 0% on Garbage Collection. I found that is't difficult to study gc behavior because I can't do a simple programming test. String o=new String("true"); String oa=new String; oa=o; o=null; oa=""; oa=null; return; How do I know when o is eligible to gc?
I got 0 questions on GC also. Below: 1. String o=new String("true"); 2. String oa=new String; 3. oa=o; 4. o=null; 5. oa=""; 6. oa=null; 7. return; Line 4 is where o is eligible for gc. It looks as if this is in a method in which case the local variables will be eligible after the method ends.
I might be wrong, but I disagree with Tom. First off, Garbage Collection questions are usually asked "When is the OBJECT created at line X eligible for GC?" "o" is a reference to a string, not an object. The object is the String with the text "true". An object is eligible for garbage collection when there are no active references to that object. In line 3, a second active reference is created when the first reference in the array oa is pointed at the string. Therefore, when o is set to null in line 4, there is still an active reference. When oa is set to "" in line 5, there are no longer any references to the string containing "true", so it is then eligible for GC, after line 5. Matt
Joined: Aug 17, 2001
Good point there. I could have sworn I got the questions right on the Test Exams. I thought it was able for GC once it changed the value to its variable. The variable to "true" in line one is one value but once it got assigned null (on line 4) it was then a new value then the o on line one is then eligible for GC and a new value for o is made on line 4.