• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literals not eligible for GC?

 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why String literals are not eligible for GC?

 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't they? How do you know?
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand. Don't know. Where did you read about memory management and the String pool?
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gian Franco wrote:Have a look at this webpage


Thanks for the link Gian.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic