• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Garbage collection

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the earliest line in the following code after which object b
will be a candidate for being garbage collected.
public class test{
Static string f()
{
String s="hello";
String b="bye"; //1
String c=b+"i";
String d=b;
b=a; //2
d=a; //3
return c; //4
}
public static void main(String s[])
{String msg=f();
System.out.println(msg); // 5
}
}
can any one tell the answer with explanation
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi George,
Moving this to the Certification Study group as it's not related to results.

------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer would be that that object "bye" is eligible only at the end of the method. Please correct me if I am wrong.Here is why:
String s="hello";
String b="bye"; //1
String c=b+"i"; - c refers to bye + "i"
String d=b; ----- d refers to bye
b=a; //2 ------- b no longer refers to bye
d=a; //3 ------- d no longer refers to bye
return c; //4
If you see above though at line 4 we still have c referring to bye and so even if there is one reference to the object it cannot be garbage collected.
By the way I did not see where a was declared in this method???
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello George,
First make following correction in your code
public class test{
Static string f() // small 's' in Static and capital 'S' in string
{
String s="hello"; //keeping in mind your line commented as 2 & 3 i suppose that this reference variable should be 'a'
String b="bye"; //1
String c=b+"i";
String d=b;
b=a; //2
d=a; //3
return c; //4
}
public static void main(String s[])
{String msg=f();
System.out.println(msg); // 5
}
}

For Latha:
in this line by you
String c=b+"i"; //- c refers to bye + "i"
But actually c refer to new String object which is result of concatenation of b+"i" that is "byei".
So bye will be eligible for garbage collection after line 3 when there are no more reference to object "bye".

------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test{
static String f()
{
String s="hello";
String b="bye"; //1
String c=b+"i";
String d=b;
b=a; //2
d=a; //3
return c; //4
}
public static void main(String s[])
{String msg=f();
System.out.println(msg); // 5
}
}
First of all this program will not at all compile bcoz variable a is not declared.
Now if we assume that a is also a String varaible then object b will be available for garbage collection after line marked with //3, since "bye" is not referenced after d has the reference of object a.
Hope this help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic