Re: String intern question
Roedy Green wrote:
On Thu, 19 Jun 2008 16:30:08 +0200, Tommy Halsbrekk <tommy@dummy.no>
wrote, quoted or indirectly quoted someone who said :
I know the internal difference is that the first example ends up with
two different objects with the same internal literal representation of
the string value and the second ends up with two pointers to the same
object. But in some situations, for example when memory is precious,
couldn't one use example 2 instead?
interning SAVES memory by replacing a reference to a duplicate with a
reference to the single master copy. This allows dups to be garbage
collected.
Interning takes time, but it saves RAM.
The problem with intern, is that interned strings live for the duration
of the system classloader (often the lifespan of the JVM instance),
which can lead to terrible memory leaks if used improperly.
It saves ram in the case where the same string may be duplicated.
An example where it wastes space is trivial to create.
public static void main(String...args) {
for (int i = 0; i < Integer.MAX_VALUE; ++i) {
System.out.println(("foo" + i).intern());
}
}
Now, Integer.MAX_VALUE strings that will only appear once will be stored
indefinitely, leading to a memory leak, and probably an OOM on most
systems, where without the intern the program would run fine.
So, intern can save memory if you have many copies of the same string in
many places, but wastes memory if you don't. The real moral is that
don't use intern unless you have a damned good reason to. If you don't
know whether you're reason is good, then it probably isn't :-).
If you're getting OOM, and a memory profiler shows you that you have
millions of copies of "Foo1", *then* you should consider using intern.
Even then, consider other alternatives, such as a shorter lived String
pool so that you don't needlessly fill the intern pool.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>