How many objects are Garbage collected?

Hi, In the below code after line number 5, how many objects are ready for Garvage collection? Is it 9 or 8. In one of the mock exams, answer is 8. But i am having a doubt, that in for loop we are creating 9 string objectes and all are ready for garbage collection at line number 5, now how is the answer 8.

Since the 'str' string is defined inside the loop then this means it is not available outside it, therefore all the references assigned inside the loop are lost when the loop exits (thus making all the objects inside EFGC). So the answer depends totally on the number of iteration the loop can do (As Thomas suggested), in which case the answer is 9. However if the 'str' string was defined as a member variable then the last reference of it is not lost, so the answer would be 8.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!

Because of the vaguaries of the String constant pool, the real exam won't use String objects when testing GC questions. The exam WILL test you on String immutability! The exam WILL test you on knowing how many non-String objects might be eligilbe for the GC.

I have my doubts about this. Object 9 goes out of scope the moment the loop terminates so becomes elligable for garbage collection at that time. That would mean 9 objects to be garbage collected. But indeed as there are 9 String litterals there's nothing here. String litterals aren't garbage collected (wasn't that changed though recently? or not in the 1.4 spec) so there are actually 0 Objects elligable for garbage collection???.

42

Sridhar Srinivasan
Ranch Hand

Joined: Nov 07, 2003
Posts: 117

posted Feb 12, 2004 02:09:00

0

But still outside the loop, str refers to Object 9 rite?Then how come it goes out of scope? I too thought that the object collected should be 0 since str refers to string literal. Can anybody explain pl!Thanks

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

posted Feb 12, 2004 02:55:00

0

The 'str' reference is defined inside the for-loop therefore this reference won't be available outside it, so any object that 'str' refers to will EGC as soon as the loop exits. As for your question, there are 9 object EGC. You should always take "Bert Bates" consideration in terms of the eligibility of strings for garbage collection. Haven't even read my first post!!!?

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205

posted Feb 12, 2004 10:16:00

0

public class A{ public void method(){ String str = "TEST"; // 1 } public void onemoremthod(){ String str1 = "TEST"; // 2 } } HI, In the above programme, when i create a string object at line number 2, will create a new string in the pool or will it refer the same as created in the line number 1. I think at line number 2, no new object will be created, simply str1 will point to the string object created on line 1. And string and wrapper classes are immutable, hence these objects will not be garbage collected. I am not sure, above undestanding of mine is correct or not. I hope seniours can correct. Thanks in Advance,

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

posted Feb 12, 2004 11:28:00

0

narasimha , You are correct, both 'str' and 'str1' will refer to the same �TEST� object, check this code.

Strings and Wrapper classes are immutable, but what makes you think that these object are not eligible for garbage collection!!! These objects like any other objects will be eligible for garbage collection as soon as they lose their reference.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5

posted Feb 12, 2004 13:59:00

0

Strings have a special place called the String literal pool, they aren't treated like other objects in terms of GC, I swear I'm not kidding on this :roll: Vicken - In your code, if s1 == s2 then we know that we have two reference variables referring to the same object, right? This object is in the String constant pool, not the heap where other objects live. The GC is only concerned with the heap. I believe that certain VMs may handle the String constant pool differently, and you might be able to deduce rules for a particular VM, but for the exam - the GC's relationship to Strings is not tested. [ February 12, 2004: Message edited by: Bert Bates ]

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

posted Feb 12, 2004 15:59:00

0

Yes, Bert you are correct, I am backing you 100% as my 2nd post suggests, but for the sake of simplicity my previous post was not that accurate.

I don't think we can ever get an answer from Bert regarding String GC. Someone posted similar question using string for GC before and I did in fact ask Bert for an answer to the guestion. Unfortunately, Bert advised me to change the string to other type then only answer will be given. So I guess we could just take Bert's adivce and move on from here. IMHO, the correct answer (if not string type) would be 9. Cheers.

Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8898

5

posted Feb 17, 2004 09:16:00

0

CH - You're right, but I'm not doing it to be cranky I've made it my personal mission to try to keep the threads in this forum focused on the SCJP exam, I know, radical, but I'm thinking "What the heck, it IS a cert forum after all". So, if I ever wandered over to JIG intermediate or advanced, and someone were to ask about the GC's relationship to Strings, I might have to jump in, but not in this forum! aloha, Bert (the focus police)