• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mughal rasmussen

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?.
 
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
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.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is string literal and so never Gced
 
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
How about 'c'? 'C' is still holding reference for 'bye'. I think the object will be garbage collected only after line marked 4.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
<code><pre>
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
</pre></code>

[This message has been edited by Jim Yingst (edited February 23, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good call. Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic