File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question" Watch "GC question" New topic
Author

GC question

Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
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.

Jerson
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
And also say s1 is garbagecollected before line 7 , how do we explain the existance of s1 in line 7?
thankyou
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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.
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Satyavani:
(IMO)
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.
Regds.
- satya
Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
Thanks Maha Ana and Mani...
Great Explanation....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question
 
Similar Threads
GC Problem
Garbage Collection and the String Pool
A GC Problem in Mock Exam
GC question..........
Garbage Collection and the String Pool