Re: Strings...immutable?
Lew wrote:
Mark Thornton wrote:
Actually it won't get garbage collected unless the class is also
garbage collected. There is, in effect, a hidden reference from the
class to all literal Strings declared in it.
Mark Space wrote:
Where did you hear this?
From the JLS.
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5>
String literals-or, more generally, strings that are the values of
constant expressions (??15.28)-are "interned" so as to share unique
instances, using the method String.intern.
Ah! Ok, string literals. I miss-read Mr. Thornton's original statement
and assumed that he meant all strings, not just literals. This makes
much more sense.
I don't mind asking stupid questions, it's how I lurn stuff. ;-)
Mark Thornton wrote:
> Actually it won't get garbage collected unless the class is also garbage
> collected. There is, in effect, a hidden reference from the class to all
> literal Strings declared in it.
=========8< snip! ===============
>
> That used to be true, but the implementation of interning was modified
> to use WeakReference.
> Mark Thornton
So the first statement isn't really true either. If the interned
strings are using weak references, then they will be garbage collected
if the GC needs extra memory (and no other strong references are being
held to them).
So the last time my JVM yacked up a virtual hairball on the digital
carpet in the form of an Out Of Memory error, it presumably at least
considered the pool of interned strings for deletion before deciding it
was well and truly out of memory...