File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Praveen Mathur
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Praveen Mathur
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic