Author Topic:   GC question
Jerson Chua
ranch hand
posted April 20, 2000 08:17 AM             
1 class A{
2 static String s1= new String("first");
3 static String s2= new String("second");
4 public static void main(String[] args){
5 s2+=s1;
6 s1=null;
7 s1=s2;
8 System.out.println(s1);
9 }
10 }

when is the object referenced by s1 will become eligible for garbage collection
A. before line 6
B. before line 7
C. before line 8
D. before line 5

The given answer is B. Isn't it only at line 7, the object referred by s1 is eligible for gc since line 6 makes s1 points to null? So I think the answer should be C which means at line 7, the object is eligible for gc.

Please verify my answer.
thanks in advance.


posted April 20, 2000 09:50 AM           
Jerson ,

The question asks when will s1 be eligible for garbage collection.
With the given options best one which is matching with the correct answer is before line 7.
ie, after line 6 is executed.

posted April 20, 2000 10:20 AM           
I would also want to know if the string is immutable (that means it cannot be changed ) first of all how can it be assigned to a null value?
can some one clear my doubt?

posted April 20, 2000 10:28 AM           
And also say s1 is garbagecollected before line 7 , how do we explain the existance of s1 in line 7?


maha anna
posted April 20, 2000 10:33 AM             
Jerson Chua,
I want you to understand one point. It is taken for granted all the time.

1. String s1=new String("java");
2. s1 = null;
3. code 1
4. code 2
5. code 3

What does the 2nd line of statement do? It erases the s1 reference's value right?. Which mean erasing is being done at line 2, and erasing is OVER after execution if the statement at line 1. Do you agree with this? In the grayed area of GC mechanism in Java, The bare minimum info for sure , specified by JLS is this.
When an object looses all its refernces in the program, hangs in the heap without any reference point to it , it is ELIGIBLE for GC .

So setting a reference to null is in another way of saying I don't need this object anymore. We can also indicate that the object is not of interest any more by means of making the reference to refer to another object.

The more interesting point in these set of code is here , BOTH way of saying that the object is not of interest is done. But note that setting a ref to null itself ENOUGH. The referred object is ELIGIBLE FOR GC in there is no more ref pointing to it. Obviously the above set of code there is NO MORE PREVIOUS ref pointing to the object 'first'.

So the anser is correct. The 'first' object is ELIGIBLE for GC after line 6. That is before line 7.

maha anna

ranch hand
posted April 20, 2000 12:48 PM             



Strings are immutable. After having said that, we are not
chainging the string object, we are chainging the referece
to that string object (s1) to be null.

For your second posting:

we are not garbage collecting s1, we are garbage collecting
the object referenced by s1.

Hope this helps.


- satya

Jerson Chua
ranch hand
posted April 20, 2000 07:12 PM             
Thanks Maha Ana and Mani...
Great Explanation....