• Post Reply Bookmark Topic Watch Topic
  • New Topic

About GC  RSS feed

 
Anand M S
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have bit confusion about GC, pl help me

String objects which are not created by new operator are not eligible for GC? i.e Strings created in string pool are never be eligible for GC
Example:
String str = "Hello";
str = null;
str = "srisri";
String "Hello" will be eligible for GC?
or the strings which are created by the new operator only eligible for GC?

Thanks,
Anand
[This message has been edited by Anand M S (edited February 23, 2000).]
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strings which are created by the new operator are the only ones eligible for GC.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = (String) a.clone();
e = null; // "Wallace" (2nd) could be collected
</pre></code>

[This message has been edited by Jim Yingst (edited February 23, 2000).]
 
Anju Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
In your example, if you had code as following:
System.out.println(a.substring(3));
Since a temporary string has been created above, it will not be garbage collected, right? :-(
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it will. It's possible that the JVM may be smart enough to somehow mark it for some sort of "immediate gc" rather than "regular gc" - but one way or another, there's a String object on the heap which has no references any more, and the JVM is allowed (though not guaranteed) to reclaim it. That's within the definition of "garbage collection".
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, I was writing in terms of the SCPJ2 exam...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough. As usual I'm obsessed over the nit-picky details that aren't on the exam. For the exam they don't really need to know about string literals at all, I think - but when the subject does come up, I figure it's just as easy (and more accurate) to say "all Strings except literals can be GC'ed", rather than "only Strings created with new can be GC'ed." I see now that you weren't really making a general statement here - I had just made this same point on another thread in a slightly different context, so I thought I'd cut & paste my response here as well.
 
Anand M S
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, Tony, Rao thanks to one and all
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!