Author Topic:   Mughal rasmussen
Mahen
greenhorn
posted February 21, 2000 09:07 PM             
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?

public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}


No clue! detailed explanation about GC questions like above

Umesh
ranch hand
posted February 21, 2000 10:07 PM             
After line marked 2.
Till line 2, b has ref. to 'bye'.
At that line 'bye' lost the ref. to b and hence eligible for GC.

Am I right ?.

Hood
unregistered
posted February 21, 2000 11:14 PM           
The object "bye" will be garbage collected after //3. This is because after the statement "String d = b;" both d and b are refering to the same object that is "bye". So after //2 b will be refering to "hello" but d will still be refering to "bye". After //3 there will be no reference to "bye" hence it will be ready for garbage collection.

shan
unregistered
posted February 22, 2000 08:16 AM           
It is string literal and so never Gced

akhil
unregistered
posted February 22, 2000 01:32 PM           
How about 'c'? 'C' is still holding reference for 'bye'. I think the object will be garbage collected only after line marked 4.

Jim Yingst
sheriff
posted February 22, 2000 02:39 PM             
akhil- No, c is holding a separate String, "bye!". It was created by copying characters from the original "bye" and adding "!", but now it's a completely separate String, and has no link to the original "bye".

shan- correct, since it's a String literal, it won't ever be garbage collected. That's a subtle point that isn't on the exam, but it's good to know anyway.

If the object were not a String literal, then it would be eligible for collection after line 3, as Hood says.

Hood
unregistered
posted February 23, 2000 01:37 AM           
Thanks Jim, when we create a string using a literal it creates a String object and we can apply all the methods of a String class on it (like "hood".equals("hood") is a valid statement) so I thought it would behave as a normal object during garbage collection also. But it seems Garbage collection takes care of objects created by the "new" operator only.

Jim Yingst
sheriff
posted February 23, 2000 11:05 AM             
Hood- not exactly. There are numerous ways to create an object without "new" - at least, without a "new" in any code that you write or see - and garbace collection works normally for all of them, except for string literals. Examples:


String a = "Wallace"; // "Wallace" never collected
String b = a + " & Gromit";
b = null; // "Wallace & Gromit" could be collected
String c = a.toUpperCase();
c = null; // "WALLACE" could be collected
String d = a.substring(3);
d = null; // "lace" could be collected
String e = a.clone();
e = null; // "Wallace" (2nd) could be collected

[This message has been edited by Jim Yingst (edited February 23, 2000).]

maha anna
bartender
posted February 23, 2000 12:52 PM             
one small correction.But I understand your point.

String e =(String) a.clone();
e = null; // "Wallace" (2nd) could be collected

// the clone() method inherited from Object returns an Object. We have to cast to the corrs. type.
maha anna

[This message has been edited by maha anna (edited February 23, 2000).]

Jim Yingst
sheriff
posted February 23, 2000 01:25 PM             
Good call. Thanks.

|