wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Praveen Mathur
Greenhorn

Joined: Sep 21, 2003
Posts: 18
Hi Friends,
How many of String object created in line 5 are eligible for garbage collection at line 8?
1public void A()
2{
3 for(int i=0; i <=10 ; i++)
4 {
5 Sting str=Integer.toString(i);
6 System.out.println(str);
7 }
8 System.out.println("Ready for garbage collection");
}
Answer written is 10.
I think it should be 11. Any Comments?


"The self-controlled soul, who moves amongst sense objects, free from either attachment or repulsion, he wins eternal Peace." - Bhagavad Gita
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi,
I think I have to agree with you. 'str' is inside the scope of the for loop that means that outside the for loop all objects are eligble for gc. maybe the question overlooked the <= part of the for loop? if only < was used then it would be 10. but now it seems to be 11.
Rikko
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Because of the vagaries of the String pool, it would be better to restate the question using a different type of object. The real exam will NOT mix GC eligibility questions with String objects.
Hope that helps


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
I was re-thinking my previous reply and was wondering how the string pool affect things. Does anyone know what exactly happens with the stringpool in this case? it seems to me that either all objects are GC-ed, or that all string objects are saved in the pool. In that case the GC doesn't remove any objects at all.
Rikko
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In this case no string object was interned, thus the String pool does not intervene.
This example has even a more dangerous reason in order not to be in the exam.
The example is asking you, sir, dou you know if every JVM implementation happens to nullify a reference right after it goes out scope?


SCJP2. Please Indent your code using UBB Code
Praveen Mathur
Greenhorn

Joined: Sep 21, 2003
Posts: 18
Jose, we have now 3 situations
(1) if "Garbage collection of out-of-scope local variable is JVM dependent".
then question is wrong and should have some other option to select.
(2) if "Garbage collection of out-of-scope local variables (like str) is done after the method A() returns not at the line 8" then answer should be 10 because at line 8 one str variable(even out-of-scope) is still alive.
(3) if "Garbage collection of local variables (like str) is done once they are out-of-scope" then answer should be 11.
Now question is, which among these 3 is true?
Vivek Mongolu
Ranch Hand

Joined: Apr 17, 2003
Posts: 42
I think the answer 10 is right.
there are actually 11 string created in the for loop , but str points to the last string which is "10". So since str is referrring to this string , it cannot be garbage collected. So 0 - 9 can be reclaimed which makes the total no of string that can be garbage collected to 10.
Correct me if I am wrong
Vivek
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage Collection
 
Similar Threads
doubt regarding garbage collection
question on GC frm go4java mock test
How many objects are Garbage collected?
Explain the output for garbage collection
Garbage collection