Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are string literals garbage collected?

 
Jonatan Samoocha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After having consulted several books and threads on this forum, it is still unclear to me how the garbage collection mechanism works on String literals. More generally: Do objects in the literal pool become eligable for garbage collection? (before the JVM exits, of course).
For example, in the following sample:

does the string literal at the marked line become eligable for garbage collection? If so, at which line?
Can anyone point me to detailed rules about this topic? Thanks in advance,
Jonatan
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are never garbage collected. The JVM specification will tell you that String literals are part of the runtime constant pool, which is distinct from the heap where the garbage collector does its job.
- Peter
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter i do agree with you at this point that String Literals are Not Garbage Collected.As mentioned clearly by JLS.But the problem still exist that why many mocks including some good mocks ask the questions like the one which Jonatan mention.What is the solution.And does SUN can ask or asks this type of questions?.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikrama, Sun's exam will conform to what the JLS says, don't worry ! The questions in the mocks that don't are bad questions and you shouldn't pay attention to them
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello EveryBody
To see how String literals are not eligable for g.c make a WeakReference to point one of them, call the g.c. and later call get on the WeakReference to see how it is still returning a reference to the String literal.
By the end of October I posted an example, search by Botella to see it.
Hello Peter
The CONSTANT_String_info in the constant pool is not a String literal, neither a String, it is not an object. It is a symbolic reference that points to the content of a String object that will be created when the JVM resolves the constant pool entry. That is refered to in the JVMS as "derive".
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i did come across these issues and many times i got it wrong
when i choose "string litreal wont be gc'ed" since
the mock exams keys says "the string literal will be gc'ed" becoz they are created using new operator for the first time and optimitized later on...
Thankx for bringing it up and clarifying it
Ragu
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh. Thanks Jose - spot on.
 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, wait, does that mean that:
String string = "hello";
is not gc'd and
String string2 = new String("hello");
is gc'd?
Wouldn't make sense because the first is just shorthand for the second. Am I missing a point?
BTW, the Syngress Java cert book (for which I can post many hundreds of mistakes) makes a point of having the user learn when String objects are gc'd.
But I will take JavaRanch's word for it any day

Caroline
Originally posted by Ragu Sivaraman:
Yeah i did come across these issues and many times i got it wrong
when i choose "string litreal wont be gc'ed" since
the mock exams keys says "the string literal will be gc'ed" becoz they are created using new operator for the first time and optimitized later on...
Thankx for bringing it up and clarifying it
Ragu

 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Caroline, that's what it means. The second is not just a shorthand for the first. In the first, you assign a reference to the string constant "hello" to string. In the second case, you construct a new String object (which will be allocated on the heap) the contents of which are identical to that of the "hello" string constant.
Taking that knowledge as a starting point, try to predict the output of the following:If you're sceptical, try it...
- Peter
 
Wagner Danda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to remember, Strings also have the intern() method:
String sA = "hello";
String sB = new String("hello");
String sC = new String("hello").intern();
System.out.println(sA == sB); // false
System.out.println(sA == sC); // true
Wagner Danda
Originally posted by Peter den Haan:
[B]Yes, Caroline, that's what it means. The second is not just a shorthand for the first. In the first, you assign a reference to the string constant "hello" to string. In the second case, you construct a new String object (which will be allocated on the heap) the contents of which are identical to that of the "hello" string constant.
Taking that knowledge as a starting point, try to predict the output of the following:If you're sceptical, try it...
- Peter[/B]

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GC is for Heap
String lireals are in constant/litreal pool
So GC wont help there
Ragu
 
Caroline Bogart
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be clear... are all String values known at compile-time "constants"? And are they therefore in the literal pool and ineligible for GC? e.g., any string created at run time is eligible for GC after all references to that String are null?
Thanks.

Originally posted by Peter den Haan:
[B]Yes, Caroline, that's what it means. The second is not just a shorthand for the first. In the first, you assign a reference to the string constant "hello" to string. In the second case, you construct a new String object (which will be allocated on the heap) the contents of which are identical to that of the "hello" string constant.
Taking that knowledge as a starting point, try to predict the output of the following:If you're sceptical, try it...
- Peter[/B]

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Caroline that's right !
Although there is a little thing to add.
String created at runtime (i.e. with the new keyword) can be interned into the private String pool by invoking intern() on the dynamically created String object. intern() returns a reference to the String in the String pool.
For instance,
String st = new String("test"); // test is not in the pool
st=st.intern(); // puts the String inside the String pool and returns a reference to it.
Now if you dereference st (st=null) the String "test" will still be in the String pool after garbage collection.
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic