• 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

string pools

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really a dumb question but still then curious about the
answer.
class strng
{
public static void main(String args[])
{
String s;
int i = 0;
do{
//objects created in the string pool
s="I think i am in the pool with others "+i;
++i;
}while(true);
}
}
now since only objects created with the new operator will be garbage collected,each object created in this loop will remain
uncollected.Is'nt it likely that at some point the whole memory will be filled with just objects or is there any security feature that avoids this.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm moving this thread to the beginner section where it is more appropriate.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...only objects created with the new operator will be garbage collected..."
Untrue. This is a myth that seems to be spreading recently. It's wrong. The short answer is that all the objects created in the program are eliglble for GC once s goes out of scope. The long answer about exactly which Strings will "never be garbage collected" is a bit involved for the "beginner" forum, but I'm not moving the thread again (actually I can't, once it's in a forum I don't moderate) so y'all can just ignore this if it's too involved:
Forget about whether the new operator was used - that isn't what determines whether a string can be collected. There are many ways to create new strings without a visible "new" operator, and almost all of them create an ordinary String which can be collected. The only exceptions are:
  1. Strings which appear as literals in the code without any other operations being done to them, e.g.:
    <code><pre> String a = "literal";</code></pre>
  2. Strings which are the result of a constant-valued expression, e.g.
    <code><pre> String b = "not a " + "literal" + " but a constant";</pre></code>
    or
    <code><pre> String c = "Pi equals " + Math.PI;</pre></code>
    (because Math.PI is final, it's considered constant)
  3. Strings returned by the intern method, e.g.
    <code><pre> String d = anotherString.intern();</pre></code>

  4. Cases 1 and 3 should be easy to identify; case 2 is a little harder - you have to look at each part of an expression to determine if it's a constant, i.e. if it's a variable, it needs to be final.
    All the above examples are put in the intern pool, and can never be collected. All other kinds of strings are "normal" and can be collected (if there are no more references). So in this example:
    <code><pre> String s = "good"
    s+= "morning";
    s = null;</pre></code>
    "good" is a literal and can never be collected, but "goodmorning" can be collected (s += "morning" is equivalent to s = s + "morning", and since s isn't a constant, the expression s + "morning" isn't a constant expression).

    [This message has been edited by Jim Yingst (edited February 26, 2000).]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Jim, you have said that Strings which appear as literals and constants and the String values returned by the intern method are placed in the Intern pool and can never be GCed.
My questions are :
1. Why are these string literals need to be kept, if they are
not being referenced in the program?
2. If these string literals are collected and kept in the Intern
pool, how long will they be existing?
Thanks..
 
Suresh Ray
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody answer the above question please?
Thanks....
 
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
You almost never really lose all references to string literals and constant expressions (note I'm not including strings returned by the intern() method here). Whatever code fragment they're used in, the JVM never knows when you're going to call that method or constructor again, and you'd need that String object again. So it keeps them all. The only times the system could be sure a given String literal / constant is no longer needed are: (a) if the string constant is only used in a staic initializer or static variable initialization expression, which is only run once when the class is loaded, or (b) if all references to all instances of the class have been lost, and the class itself is garbage collected (not just the individual instances). These cases are sufficiently rare that it's not worth the JVM's time to try to do a little extra GC, in my opinion.
As for strings returned by the intern() method - it is possible to lose all references to those strings, other than the one that's kept in the string pool. There isn't really any good reason I can think of for the string pool to continue keeping that last reference, other than the fact that the Java Language Specification makes no provision for ever removing anything from the intern pool. It can be argued that the JLS doesn't expressly forbid it either, but I think this is ignoring the clear intent of the wording used by the JLS. In any event, I know of no JVM implementation that does ever remove anything from the intern pool - if there is one, I'd like to hear about it.
So, the end result is that anything put in the String intern pool will be there as long as the JVM continues running. This isn't normally a problem - string literals would have to be stored in memory somewhere anyway. But it is something to keep in mind if you're calling the intern() method very often yourself - you can fill up memory this way, and it's impossible to free afterwards.
 
reply
    Bookmark Topic Watch Topic
  • New Topic