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
Joined: Aug 30, 2000
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).
I am thinking all string objects str0, str1, str2, str3 will be available for GC, after line 4. any comments? Thanks Santosh Jaiswal
Joined: Sep 23, 2000
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.
Joined: Sep 28, 2000
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.
Joined: Mar 17, 2000
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).