How many objects are elgible for garbage cllection at the end of line 4. I say None. Can somebody please explain the answer. Thanks Sunita
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Sunita, Think the following 3 items are available for garbage collection: str1 - set to 'null' in Line 2. Any object set to 'null' is eligible for garbage collection. The literal strings "second string" and "third string" as they are no longer being referenced. (Not 100% sure of these, as they would be in the string pool and I'm not clear on gc and the string pool).
Hi, I think all are String literals...and they should be saved in String pools. There is no new String() created so no String object gc ed?
Khurram Fakhar
Ranch Hand
Joined: May 29, 2000
Posts: 65
posted
0
I agree with Ricky
Regards Khurram
Kalpesh Pandya
Greenhorn
Joined: Oct 07, 2000
Posts: 14
posted
0
I am bit confused. Why GC is not considering String pool ?
Manish Singhal
Ranch Hand
Joined: Sep 21, 2000
Posts: 104
posted
0
Hi Friends, Please see following URL --- http://www.javaranch.com/maha/Discussions/Garbage_Collection/Strings_-_Khalid_-_JavaRanch_Big_Moose_Saloon.htm Since String pool is not in the objectives of SCJP ( as mentioned in the above link) so.... I think there will only be two objects available for GC. i.e. "second string" and "Third string". Maha Anna please say something about this code. regards, Manish [This message has been edited by Manish Singhal (edited October 08, 2000).]
Santosh Jaiswal
Greenhorn
Joined: Oct 04, 2000
Posts: 26
posted
0
I am thinking all string objects str0, str1, str2, str3 will be available for GC, after line 4. any comments? Thanks Santosh Jaiswal
mohit joshi
Ranch Hand
Joined: Sep 23, 2000
Posts: 243
posted
0
I would agree with Manish. also Jane, even if we are setting str1 to null in line 2, the String object that str1 was refering to ( ie. "some string") is still being refered to by str0 now. So that object will not be Garbage Collected.
Shameen MK
Greenhorn
Joined: Sep 28, 2000
Posts: 22
posted
0
String str0 = null; String str1 = "some string"; String str2 = "second string"; String str3 = "Third string"; str0 = str1; // 1 str1 = null; //2 str2 = str0; //3 str3 = str2; // 4 According to me, at line 4 , this will be the case: str1 str0 str2 str3 | \ | / null Some string therefore, second string and third string will be ready for garbage collection.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
For your information, string literals( constructed using the = operator instead of new() ) are stored in the String pool. These strings are re-used if the program tries to create identical strings. Strings in the string pool live longer than the objects created using the new() operator. The JVM garbage collects the strings in the pool when the class is unloaded. Please note that you are not required to know about GC of string literals for the exam. The GC-related questions in the exam almost always deal with regular new()'d objects. However, if in case questions do appear about GC of string literals, treat them as any regular object. Hope that helps, Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
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.