*
The moose likes Beginning Java and the fly likes String literals not eligible for GC? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String literals not eligible for GC?" Watch "String literals not eligible for GC?" New topic
Author

String literals not eligible for GC?

dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Why String literals are not eligible for GC?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Aren't they? How do you know?
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
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.

Scenario2:



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?).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Don't understand. Don't know. Where did you read about memory management and the String pool?
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Have a look at this webpage

Here they say:

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...

"Eppur si muove!"
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Gian Franco wrote:Have a look at this webpage


Thanks for the link Gian.


Regards,
Avishkar Nikale
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literals not eligible for GC?