# How many objects are Garbage collected?

Narasimha Rao B.
Ranch Hand
Posts: 205
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.

1 public void method(){
2for(int i =1;i<10;i++){
3String str=Integer.toString(i);
4}
5System.out.println("out of for loop");
6 }

Thomas De Vos
stable boy
Ranch Hand
Posts: 425
I'm trying to help here with a question: How many iteration of the loop are done?

Vicken Karaoghlanian
Ranch Hand
Posts: 522
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.

Sridhar Srinivasan
Ranch Hand
Posts: 117
Hi!
The string is created in the loop as follows:
loop from 1 to 9.So it creates
str="1" Object1
str="2" Object 2 (Object 1 collected)
str="3" Object 3 (Object 2 collected)
str="4" Object 4 (Object 3 collected)
str="5" Object 5 (Object 4 collected)
str="6" Object 6 (Object 5 collected)
str="7" Object 7 (Object 6 collected)
str="8" Object 8 (Object 7 collected)
str="9" Object 9 (Object 8 collected)
Hi!
I have a question related to this.I read somewhere that the string literals are not Garbage collected.The above is a string literal rite?
The above is not as str=new String("value")-If this is the case, I accept that the value is 8.Can anybody explain pl!Thanks

Bert Bates
author
Sheriff
Posts: 8900
5
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.

Sridhar Srinivasan
Ranch Hand
Posts: 117
How abt the answer for the above question?Anyway, it's using string literal rite?I want to know that the above question is rite r not please?Thanks

Jeroen Wenting
Ranch Hand
Posts: 5093
Originally posted by Sridhar Srinivasan:
Hi!
The string is created in the loop as follows:
loop from 1 to 9.So it creates
str="1" Object1
str="2" Object 2 (Object 1 collected)
str="3" Object 3 (Object 2 collected)
str="4" Object 4 (Object 3 collected)
str="5" Object 5 (Object 4 collected)
str="6" Object 6 (Object 5 collected)
str="7" Object 7 (Object 6 collected)
str="8" Object 8 (Object 7 collected)
str="9" Object 9 (Object 8 collected)
Hi!
I have a question related to this.I read somewhere that the string literals are not Garbage collected.The above is a string literal rite?
The above is not as str=new String("value")-If this is the case, I accept that the value is 8.Can anybody explain pl!Thanks

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???.

Sridhar Srinivasan
Ranch Hand
Posts: 117
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
Posts: 522
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
Posts: 205
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.

Vicken Karaoghlanian
Ranch Hand
Posts: 522
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
Posts: 8900
5

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
Posts: 522
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.

CH Lee
Ranch Hand
Posts: 42
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
Posts: 8900
5
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)