File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Garbage Collection and the String Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Garbage Collection and the String Pool" Watch "Garbage Collection and the String Pool" New topic

Garbage Collection and the String Pool

Brian Sarka

Joined: Dec 09, 2003
Posts: 3
I am curious to know what happens in this situation - see the code, and my comments regarding whether or not a String in the String Pool is reusable after it no longer has a reference, but has not yet been gc()'d:
public class findString
public static void main(String[]args){
String s1 = new String("stringOne"); //new object on the heap
String s2 = new String("stringTwo"); //new object on the heap
String s3 = s1; //new reference to the existing object on the heap
s2=null; //the object that contained the string "stringTwo" is
// now 'orphaned'
s2=s1; //the reference 's2' is now pointing to the same object as
// the reference 's1'
s3="stringTwo"; //does s3 point to the previously orphaned String
// object in the String pool, or does this line instantiate a new String
// object? or does this depend on whether or
// not the garbage collector has already picked up the old String object?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

There is indeed a "String pool" that holds references to all literal (i.e., double-quoted) Strings that appear in a program. By default, Strings you create yourself with "new" are not put into this pool, or otherwise shared with the pool.
If you call String.intern() on a dynamic string, and there's a string with the same contents in the pool, it will be returned; otherwise, a reference to that string will be added to the pool.
Every appearance of a given literal string in a program refers to the same String from the string pool; strings from the pool are not garbage collected.

[Jess in Action][AskingGoodQuestions]
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Welcome to JavaRanch!

In line 4, two objects are created. One is placed in the StringPool, and one is a copy of that string. It is the copy of that string which s1 references. We'll call these objects pool1 and notPool1 (So s1 is a reference to the object notPool1, and pool1 is an object existing in the StringPool).
In line 5, you are doing the same. We'll call these object pool2 and notPool2 in a similar fashion.
In line 6, you set s1 to refer to the object notPool1.
In line 7, you set s2 to refer to nothing. notPool2 now how no references and is eligible for garbage collection. However, the object pool2 still exists in the StringPool (the StringPool contains a reference to that object), so that object is not eligible for garbage collection.
In line 9, you set s2 to refer to the object refered to by s1 (which is, in this case, pool1)
In line 12, you set s3 to refer to the object pool2 (which is residing in the StringPool). Note that s3 is referring to a different object than s2 was referring to in line 5. You can test this with the following code:

This will print out false, because the object pool2 is not the same object at notPool2.
So, at the end of this, you have 4 objects created, 1 of which is eligible for garbage collection.
[ December 09, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the intermediate forum...
[ December 09, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Garbage Collection and the String Pool
It's not a secret anymore!