aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection" Watch "Garbage collection" New topic
Author

Garbage collection

george tharakan
Greenhorn

Joined: Jan 12, 2001
Posts: 7
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
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
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???
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
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.
-------------------------


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
Amit, Jhalani
Greenhorn

Joined: Jan 11, 2001
Posts: 21
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection