• 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 String literals ever eligible for garbage collection?

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the objects created in the String pool ever eligible for garbage collection?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No unless program gets out of scope,I guess!!
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Randall,
I think so. Consider,
String str = "Hello";
Here the string literal is implemented as an anonymous String object. So even though we are not using the new operator it is internally implemented as a anonymous String object so like all other objects it should be eligible for GC(provided no other String references in the pool are denoting the same object)
Please let me know if I am wrong.
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings created without using the new keyword are NEVER garbage collected. Even if there are no references to them. All such strings go into the String pool and just sit there till the whole program ends (ie. the JVM).
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Ash Rai
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
I may be wrong but this concept is really tricky. If the String objects which are created without the use of new are NEVER GC then what is the meaning of questions such as the following:

Which is the earliest line in the following code after which the object created on line 0 will be a candidate for being garbage collected?
public class Question {
static String f() {
String a = "hello";
String b = "bye"; // line 0
String c = b + "!"; // line 1
String d = b; // line 2
b = a; // line 3
d = a; // line 4
return c; // line 5
}
public static void main( String args[] ) {
String msg = f();
System.out.println( msg ); // line 6
}
}
This code fragment is from Khalid's mock exam.
The answer is line 4.
What is the meaning of 'candidate for GC' when they never fit into the defination of objects which can be Garbage collected?
Can you please elaborate on this topic.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If objects on the string pool never get cleaned up then you would definitely experience disk thrashing if a program has been running for some time. I think that some of the stuff gets cleared off when the runtime system starts getting nervous about memory.

 
Paul Anilprem
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. It does not. However crazy it may sound but this really is a problem and I have experienced it in last project that I worked on. We were observing that the JVM's size was gradually increasing with time and after 8-10 hrs it was crashing with an exception "Not sufficient memory". Then we realized that we were creating big XML message strings by concatenating and they were never released. Even other teams were have the same problem and it was an enterprise wide issue (it was a "Very big" project with a very big company). The resolution was to use StringBuffers instead.
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited December 25, 2000).]
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String literals( string objects that are not new'd ) are a part of the constant string pool that is maintained on a per-class basis. Unlike the objects that are created on the heap which gets garbage collected using a "no-active-reference" logic, the objects in the constant pool are not GC'd until the class gets unloaded.
The JVM spec suggests, but not mandates, that the constant runtime pool for all classes be cleaned up when the class is unloaded by the JVM. However, since this is not a must-have feature, there are several implmentations of VM which ignores this requirement leaving behind many unclaimed objects.
FYI, there are some implementations of VM that are written for typically small devices such as wireless phones or PDAs, where memory is an expensive resource, that actually follow this requirement and sweep even the constant runtime pool before unloading the class.
You may want to checkout the VM spec about the details of the constant runtime pool incase you are curious.
Hope that helps
Ajith
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OIC. Thanks for the tip. Now I see it. I was just monitoring the memory usage here. I have a JTable that is populated with the result set from a query. For reports I am writing out html so there is a lot of concatenation with td tag + cellvalue + td tag etc. I saw the memory usage gradually grow and then it peaked at a certain level. Thats probably because of re-use from the pool. This can probably be handled. But it seems the JTable is gobbling memory everytime a new query is run.
This never gets released.
You can look pretty silly if you cause the user's machine to hang (or worse crash the server) . Fortunately this is the last and smallest module in a phased delivery project. So I still have time to have a serious re-think about java and scrap what was done upto now.
Sahir
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ajith,
Where can I find the VM specification which would indicate this. I looked up at the one on the SUN's website.
Thanks
Varsha

Originally posted by Ajith Kallambella:
String literals( string objects that are not new'd ) are a part of the constant string pool that is maintained on a per-class basis. Unlike the objects that are created on the heap which gets garbage collected using a "no-active-reference" logic, the objects in the constant pool are not GC'd until the class gets unloaded.
The JVM spec suggests, but not mandates, that the constant runtime pool for all classes be cleaned up when the class is unloaded by the JVM. However, since this is not a must-have feature, there are several implmentations of VM which ignores this requirement leaving behind many unclaimed objects.
FYI, there are some implementations of VM that are written for typically small devices such as wireless phones or PDAs, where memory is an expensive resource, that actually follow this requirement and sweep even the constant runtime pool before unloading the class.
You may want to checkout the VM spec about the details of the constant runtime pool incase you are curious.
Hope that helps
Ajith


 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have two questions
1)Are String literals used in instance String variables treated differently then those in local variables. As far as I know, the local variables are allocated memory when the function is called, and the for instance String variables, the literals are stored in the class file itself. Thus, the instance Strings are GCed when the class is unloaded or vm stops, but I think local Strings are GCed when out of scope.Pl correct me if I am wrong.
(From exam point of view, you are not supposed to know GC wrt String Literals)

2)What about the Pooled - Strings which are created when you create a String with new keyword. As / RHE, when you use new keyword, you also make a String in the pool (So two different String objects). Now when the String object gets GCed, does the Pool - counterpart also gets GCed?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic