• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

GC

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just befor line 7 ... because the data in refernce of "a" is changed and is no longer required.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic