This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are String literals ever eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are String literals ever eligible for garbage collection?" Watch "Are String literals ever eligible for garbage collection?" New topic
Author

Are String literals ever eligible for garbage collection?

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Are the objects created in the String pool ever eligible for garbage collection?


SCJP
Visit my download page
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
No unless program gets out of scope,I guess!!


I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
Ash Rai
Ranch Hand

Joined: Dec 15, 2000
Posts: 34
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.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Ash Rai
Ranch Hand

Joined: Dec 15, 2000
Posts: 34
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.
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

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
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
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).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

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
Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
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

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243

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?
 
wood burning stoves
 
subject: Are String literals ever eligible for garbage collection?