String.intern() still "considered harmful"?

R

Robert Mischke

I've seen several remarks floating around the web that on some JVMs,
the use of String.intern() will lead to the interned Strings being
unreachable for garbage collection, ie the memory used by them is held
until the VM is restarted. However, I have not found such an assertion
in the regular Sun docs. In fact, I have found statements (e.g. at
www.mindprod.com/jgloss/interned.html) that this was a problem before
Java 1.2 but was resolved by the new weak references in 1.2.

Can anyone give a clear statement on what the current situation is? I
have several applications where String.intern() is a huge memory
saver, and I don't want to dismiss it just because of vague rumors.

If the problem still exists (which I don't hope), a list of the
affected VMs would be greatly appreciated.

Thanks in advance!


Robert
 
R

Robert Mischke

I've seen several remarks floating around the web that on some JVMs,
the use of String.intern() will lead to the interned Strings being
unreachable for garbage collection, ie the memory used by them is held
until the VM is restarted. However, I have not found such an assertion
in the regular Sun docs. In fact, I have found statements (e.g. at
www.mindprod.com/jgloss/interned.html) that this was a problem before
Java 1.2 but was resolved by the new weak references in 1.2.

Can anyone give a clear statement on what the current situation is? I
have several applications where String.intern() is a huge memory
saver, and I don't want to dismiss it just because of vague rumors.

If the problem still exists (which I don't hope), a list of the
affected VMs would be greatly appreciated.

Come on please, someone has got to know this... :)

Robert
 
T

Tony Morris

Robert Mischke said:
I've seen several remarks floating around the web that on some JVMs,
the use of String.intern() will lead to the interned Strings being
unreachable for garbage collection, ie the memory used by them is held
until the VM is restarted. However, I have not found such an assertion
in the regular Sun docs. In fact, I have found statements (e.g. at
www.mindprod.com/jgloss/interned.html) that this was a problem before
Java 1.2 but was resolved by the new weak references in 1.2.

Can anyone give a clear statement on what the current situation is? I
have several applications where String.intern() is a huge memory
saver, and I don't want to dismiss it just because of vague rumors.

If the problem still exists (which I don't hope), a list of the
affected VMs would be greatly appreciated.

Thanks in advance!


Robert

It is untrue.
Interned Strings will be eligible for garbage collection once the class
loader that loaded the class (with the String literals in the bytecode
(constant_pool)) becomes eligible for garbage collection.
http://qa.jtiger.org/GetQAndA.action?qids=68&showAnswers=true
Feel free to modify the example code to prove it to yourself.

--
Tony Morris
Software Engineer, IBM Australia.
BInfTech, SCJP 1.4, SCJP 5.0, SCJD

http://www.jtiger.org/ JTiger Unit Test Framework for Java
http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
http://xdweb.net/~dibblego/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top