• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Garbage collection. Help Needed.  RSS feed

 
punna Kumar
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In java Strings are immutable objects. Whenever a new string is created it will stored in �String constant pool�.

My dought is when string object will eligible for the GC?.

If String Object is garbage collected after use. What is the use of constant pool in JVM?.(My understanding is one constant pool for the JVM)

Please explain me.

Regards,
punna
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by punna Kumar:
Whenever a new string is created it will stored in �String constant pool�.


Not true. Only compile time constants are stored in it (therefore the name). (Well, and Strings created using String.intern().)

If String Object is garbage collected after use. What is the use of constant pool in JVM?.(My understanding is one constant pool for the JVM)


As far as I know, String constants are referenced from the classes they are used in (as literals), so normally they aren't eligible for gc'ing. (There are ways to make *classes* getting gc'ed, but that's rather advanced stuff.)
 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
originall posted by panna kumar:

My dought is when string object will eligible for the GC?


There are two ways in declaring string objects


The first type of string(called string literal) will have a reference to the object from the String constant pool. These objects will not be garbage collected as a reference from constant tables will always be there.

The second type of string object will be garbage collected when we say
two=null. or when it is no longer in use.
[ December 26, 2006: Message edited by: ravi kotha ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ravi kotha:

The second type of string object will be garbage collected when we say
two=null. or when it is no longer in use.


The second declaration actually creates *two* String objects: The String literal in the constant pool again (the one you use as constructor argument), and the one created at runtime by using the constructor.

And a little bit of nitpicking: The object will be *eligible for* garbage collection when it is now longer reachable from program code. That is, setting two=null is neither always sufficient, nor always necessary to make it eligible. And there is no guarantee that it will get gc'ed at all, even if it becomes eligible for getting gc'ed.

As far as I know, the above is even true for Strings in the constant pool. It's only that they are typically (but not always) referenced until the termination of the JVM (by the classes they are used in).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!