Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection & string literal pool

 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I am over it too.

These are my conclusions:



For your sake, can you please tell me where you think this "Pool String" lives? ...and how did you conclude that objects that are created for String literals are never garbage collected?



Strings literals can be garbage collected if and only the class loader which loaded their String class is garbage collected. This will only happen if your code do not have any other String object declared whose class is loaded by the bootstrap loader.



Prove that X.s and Y.s can refer to two different instances during runtime.
It can be done. Note that both are compile-time constant expressions (JLS 15.28) and so are inlined at compile-time.



They can refer to two different instances if they are loaded by a different class loader as long as a bootstrap class loader or any other loader have not loaded a string literal like the ones contained in X and Y.

So it is not true what I said that String literals are never garbage collected, altough it was true in the context of the initial post.

I did not changed my mind regarding the existance of the String pool and still do not know why you clamied the JLS was erroneous in that point. Yet I see the string pool as an array of references to String objects in memory. I think I never said they were not in memory.
[ February 02, 2005: Message edited by: Edwin Dalorzo ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String literal is not garbage collected, since it is a String literal, not an object. It is the object that represents the String literal that is garbage collected.

The answer to your original question is b) as per my original post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic