Re: Does java automatically intern strings?
kohlerm wrote:
knowing when Strings are interned and when new copies of the
underlying char[] is very important to avoid consuming too much
memory.
Actually, it probably doesn't make a whole lot of difference except in corner
cases.
As a rule of thumb, the JVM and API are very smart about managing String
literals, although they will be essentially permanent. That shouldn't sweat
your memory consumption unless you have a true crapload of literals.
That leaves String intermediate values and new-allocated instances. API
methods like String.substring() tend to be pretty smart about reusing existing
char [] backing arrays, but it's pretty hard, too, to predict how the
optimizer will handle situations. Inlining, loop unrolling and
common-subexpression refactoring are things it does that I can imagine
affecting String allocation, much less interning.
I suspect that most programs facing memory problems will not find interning to
be much of an issue or much help. I'm also pretty sure that there are times
when it will. And there always come those times when every last drop counts.
My concern is that the real impact on performance of different idioms is so
very often counter-intuitive.
--
Lew