• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One more question on garbage collection

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a method which creates a number of String objects in the course of printing a count down sequence. When the program reaches line 8, how many of the String objects created in line 5 are eligible for garbage collection? Assume that the System.out object is not keeping a reference.

1.public void countDown()
2.{
3. for( int i = 10 ; i >= 0 ; i-- )
4.{
5.String tmp = Integer.toString( i );
6.System.out.println( tmp );
7. }
8.System.out.println("BOOM!");
9.}

a) none
b) 1
c) 10
d) 11

I think the answer should be d=>11. But the standard answer is 10??
As the tmp is local to the for loop, once the program exits from the for loop, the last string pointed by the tmp should also be ready for garbage collection, right?

Really Puzzled...
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
really lao..
i would go for 11 if i was asked to answer.......
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is 11.

You can run this code:
class Generics {

public static void main(String ...args) {

for( int i = 10 ; i >= 0 ; i-- )
{
A a=new A();
}
System.gc();
Thread.sleep(1000);
System.out.println("BOOM!");

}
}

class A {
static int i=1;
protected void finalize() {
System.out.println("Finalized"+i++);
}
}

And if you are lucky and the JVM runs the GC Finalized1,...,Finalized11,BOOM! is print.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, for SCJP purposes it is 11. What does the author of the question say in the answers section? All reliable mock exams have explanations for the given options being correct or incorrect.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the code that you gave (although I made some changes like wrapping the sleep statement with a try-catch block, etc) and it did print Finalized 11. But, I am confused because I would think answer '10' is correct. Read the question carefully. The question says '

When the program reaches line 8, how many of the String objects created in line 5 are eligible for garbage collection?



As the string objects are created in the loop, they keep losing their references when the next iteration runs and are eligible for GC. But, just after the end of the loop, the last reference to the String object still exists which means I can use tmp (if it is declared before the loop instead of within the loop) reference after the for loop and it will be poiting to a live object which was the last object created in the loop. This object is not eligible for GC at line 8 as it can be accessible by a live thread. Only after the METHOD (countDown()) completes, will it be eligible for GC. But, I am not able to explain why it is priting Finalized 11 and then BOOM. I would think it should print Finalized1 ... Finalized10, BOOM and then Finalized 11. Actually, can you check if the tmp was declared within the loop or before the loop?

[ October 24, 2005: Message edited by: Ramesh Mangam ]
[ October 24, 2005: Message edited by: Ramesh Mangam ]
 
Joan Pujol
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The objects are created inside the for {}. Thus when the for ends the variable holding the reference is out of ambit and the object is not accessible.
reply
    Bookmark Topic Watch Topic
  • New Topic