hi Consider the following code: 1. public void method(String s){ 2. String a,b; 3. a = new String("Hello"); 4. b = new String("Goodbye"); 5. System.out.println(a + b); 6. a = null; 7. a = b; 8. System.out.println(a + b); 9. }
Where is it possible that the garbage collector will run the first time?
A.
Just before line 5 B.
Just before line 6 C.
Just before line 7 D.
Just before line 8 E.
Never in this method please explain thank you all sherin
Sagar Sharma
Ranch Hand
Joined: Aug 31, 2000
Posts: 92
posted
0
just befor line 7 ... because the data in refernce of "a" is changed and is no longer required.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
I have a doubt on this: at line 5: a + b would actually be creating a new String object "HelloGoodbye" - is this correct. If yes, after line 5, this string "HelloGoodbye" can be GCed isn't it?
Can someone please clarify this? Thanks a million -sampaths77
Amit Punjwani
Ranch Hand
Joined: Jul 10, 2000
Posts: 50
posted
0
Hi there, The answer should be C) Just before Line 7 because it after line six i.e when 'a' is assigned the null value, is object which was referencd by 'a' eligible for GC as it does not hold any references. It doesn't make a diff if 'a' is later assigned an object referenced by 'b'. The basic point is that an object is eligible for GC if there are no active references for the same. thus my ans is C
srikrish
Ranch Hand
Joined: Sep 12, 2000
Posts: 63
posted
0
I tend to agree with sampath on this question. a+b in line 5 produces a new object which is not being referred to be any varable after the execution of line 5. So, my answer would be b - just before line 6. But I have a doubt. Is the question valid? I mean, since there is no gaurantee about gc, how can u say that gc will run at given point? Could someone please explain?
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
hi my answer is just before 7. because after "a+b" it is anonymous string literal so it will not be GCied. if i were wrong please rectify. thanks ---shi
Barbara Dyer-Bennet
Greenhorn
Joined: Sep 14, 2000
Posts: 10
posted
0
I remember reading somewhere that the reference to a in the System.out.println(a+b); statement would keep the garbage collector from marking it until the end of the method... If anyone recalls the same thing or if I'm wrong, I'd like to hear about it. Thanks.
greddy
Greenhorn
Joined: Jun 03, 2000
Posts: 15
posted
0
Hi, My answer is C b'coz when we asign null value to an object then we loose the reference to that object.As soon as we loose reference then that object will becomes the candidate for gc.so i think the answer is C. greddy
srikrish
Ranch Hand
Joined: Sep 12, 2000
Posts: 63
posted
0
Folks, Refer to William Brogden's explanation in this page: http://www.javaranch.com/ubb/Forum24/HTML/004079.html The temporary string created in line 5 will be available for gc. So, answer is B. [ Ajith corrected the URL ] [This message has been edited by Ajith Kallambella (edited September 15, 2000).]
laura_zpf
Ranch Hand
Joined: Sep 14, 2000
Posts: 30
posted
0
if the answer is just before line 5, could you tell me how many elements will be collected? should it be (a+b),a,then (a+b)?please help me!
srikrish
Ranch Hand
Joined: Sep 12, 2000
Posts: 63
posted
0
At the end of line 5, only the string produced by a+b will be available for gc since a and b are still poiting to "hello" and "goodbye' resply.
softie
Ranch Hand
Joined: Aug 29, 2000
Posts: 41
posted
0
Hi all I totally agree with srikrish as i too have seen the same Q and A some where. i dont remember.. Well once the println prints the value of a+b ie hellogoodbye the gc is collected. Cheers Venu
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
OK Guys, from William Brodgen explanation to another similar question, I reason it out as follows: an unnamed object String object a+b will be created and will be eligible for GC after line 5 executes. Therefore, my answer would B. Is everybody ready to agree with this as the answer?? -sampaths77