I was trying to apply intern() on String S1 this will do a lookup for a string object on heap that is referenced by String literal pool and
will give the existing refrence.henc when we check it will be true.
Here the object that was created earlier(i.e s1 refering to) is eligible for the garbage collection.
s1-->will be eligible for garbage collection.
s-->For an effective memory management provided by String.S is not garbage collected.
This is what understand .
(we have refernces pointing to heap(home of the object) all the time,isn't this a problem?).
Joined: Oct 13, 2005
Don't understand. Don't know. Where did you read about memory management and the String pool?
The big problem with intern is once you intern a String, you are stuck with it in RAM
until the program ends. It is no longer eligible for garbage collection, even if there are no more
references to it. If you want a temporary interned String, you might consider interning manually.
However, in the most recent JVMs, the interned string cache is now usually implemented
in soft references fashion, so that interned strings may become eligible for garbage collection as
soon as they are no longer strongly referenced.
...so it depends on the JVM implementation as well...