• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Are Strings ever garbage collected?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are Strings ever garbage collected? I would have thought so, but JQ+ has a question involving GC and the explaination stated that Strings were never garbage collected. Sorry, I don't remember the question id, but I do remember that the question was classified as "very tough".
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String objects you create with a new are eligible for gc when there is no longer a reference to them, just like any other object.
For example:
String myString = new String("I am a string");
myString = null;
This code creates a new String object, then immediately sets its reference variable to null, thus making this object eligible for gc.
Now, there is a concept of a "String literal", which is the actual text between the quotes ("I am a string"). This string literal is *also* a String object in memory, BUT this object will never be gc as long as the class in which it is defined is loaded...for most cases, and for the SCJP, you can state that "string literals are never eligible for gc."
Here's a *similar* example as above, but with different results...
String myString = "I am a string";
myString = null;
The myString reference variable is set to null, but the java VM keeps its own private reference to the String object on the right. That's why it's never eligible for gc.
Rob
[ January 31, 2002: Message edited by: Rob Ross ]
 
Bob Graffagnino
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, I understand it now.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Ross:
String objects you create with a new are eligible for gc when there is no longer a reference to them, just like any other object.
For example:
String myString = new String("I am a string");
myString = null;
This code creates a new String object, then immediately sets its reference variable to null, thus making this object eligible for gc.


When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey


Corey
When you create Strings using the new operator, the strings are created on the heap, but they are not treated as string literal and they will not be included in the pool of strings. Only if you call the intern method are the string objects available to the pool of strings.
I hope this helps!!!
Amish
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amish A Patel:

Corey
When you create Strings using the new operator, the strings are created on the heap, but they are not treated as string literal and they will not be included in the pool of strings. Only if you call the intern method are the string objects available to the pool of strings.
I hope this helps!!!
Amish


Thanks, Amish. Just to confirm what you had said, I wrote this little code snippet:

Indeed, in the first case, the Strings seem to be part of the pool (since they both reference the same object) but, in the second case, two distinct String objects are created and referenced.
Thanks again,
Corey
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

Indeed, in the first case, the Strings seem to be part of the pool (since they both reference the same object) but, in the second case, two distinct String objects are created and referenced.
Thanks again,
Corey


Hi Corey
Please put this code in a main method and run it and see what you get.

Hope this helps
Amish
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:

When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey


All objects are always created on the heap, regardless of who creates them.
String literals are objects. They are also created on the heap. BUT, they are created by the classloader when it loads a class that contains String literals, and then a reference to that String object is kept internally by the Class object. So even String literals are on the heap. They are just never garbage collected.
Rob
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic