This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings and Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings and Garbage Collection" Watch "Strings and Garbage Collection" New topic
Author

Strings and Garbage Collection

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Are strings collected in the garbage collection if the original object that created them still exists but no longer references them.
Example
MyObject had a string in it that was
String myString = "Hello";
Later on myString references a different string...
myString = "Good Bye";
Will the "Hello" string be garbage collected when the garbage collector comes along?

-Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Since Strings are objects, and objects that no longer have a reference to them are eligible for collection, I would say yes. I haven't read anything that says otherwise.
Jason
[This message has been edited by jason adam (edited November 20, 2001).]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Dale
As far as I know (and someone may correct me if I'm wrong), the two Strings you create in your examples are just Strings stored in the String pool and pool Strings are not subject to Garbage Collection because you haven't actually made an Object. If you had used new to create myString then it would be subject to garbage collection.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform


Dave
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I agree with Dave,
only Strings created with the new keyword are subject to garbage collection. Other String, called String literals, are interned in the String pool and not garbage collected.

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
So knowing that, are the strings created with the NEW keyword put into the string pool as well?
-Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
If I'm not mistaken Dale, all strings created using the "new" keyword are stored in the heap, just like all other objects.
These strings will be subject to garbage collection should no variable reference them.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Thanks for the correction on the pool vs. heap. I've unfortunately seen references to this where new was not used to create the String, but instead was something like:
String s = "String" ;
System.out.println( s );
s = null ;
And it was stated that "String" is now eligible for GC. Is this then incorrect?
Jason
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Yes, it is incorrect.
If you had

String s = new String( "String" );
System.out.println( s );
s = null ;

instead, the statement would be correct.

"When you allocate space for a String using new, that object goes to the heap and is garbage-collectible once it's fully dereferenced. A static String remains in program memory no matter what. Even if you completely dereference it, it's there and will get used again if you re-assign its value. Honest!"

-- Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
That's what I get for reading books that aren't on the recommended list
Jason
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Thank you Marily.Nice explanation.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Marilyn deQueiroz:
Yes, it is incorrect.
If you had

String s = new String( "String" );
System.out.println( s );
s = null ;

instead, the statement would be correct.

"When you allocate space for a String using new, that object goes to the heap and is garbage-collectible once it's fully dereferenced. A static String remains in program memory no matter what. Even if you completely dereference it, it's there and will get used again if you re-assign its value. Honest!"

-- Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Notice that in your example, you have *two* String objects: One created using the new keyword, going to the heap, and the literal one used to create the other (the parameter to the String constructor). So there will be one String in the constant pool and a copy on the heap (until the latter gets gc'ed).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings and Garbage Collection
 
Similar Threads
is Strings in Java are not garbage-collected ?
Identifying Objects eligible to be Garbaged
how many objects are eligible for garbage collection?
Immutability ?
how many objects are eligible for garbage collection?