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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
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
posted
0
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: 9033
10
posted
0
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
posted
0
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
posted
0
Thank you Marily.Nice explanation.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.