• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Garbage Collection & Variable Scope

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After a week or so of being a passive Javaranch participant, I've plucked up the courage to actually post something. Following is a question from Exam Cram by Bill Brogden:
1. public void countDown(){
2. for (int i = 10 ; i >= 0; i--){
3. String tmp = Integer.toString(i);
4. System.out.println(tmp);
5. }
6. System.out.println("BOOM!");
7. }
When the program reaches line 6, how many of the String objects creatd in line 3 are eligible for garbage collection? (Assume that the System.out object is not keeping a reference.)
a) None
b) 1
c) 10
d) 11
The answer given is c with the explanation that even though the tmp variable is out of scope in line 6, the local variable still has a reference to the last String created.
I've read this about a hundred times and still don't get it. I thought tmp would be out of scope by line 6 as its declared within the for loop, thereby meaning all the Strings created in the for loop (11) would be eligible for garbage collection. If anyone can explain I'd be much appreciated. Also, does anyone know where theres a really solid explanation of the garbage collection process. Every question I see on it seems to rely on the use of words like 'should' as opposed to 'must' with regard to when its activated.
Cheers,
Rob.
[This message has been edited by rob mcfarland (edited October 05, 2000).]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Yeah, I also think that the answer is 11 too. Maybe the author would like to explain more?
public void countDown() {
for (int i = 10 ; i >= 0; i--) {
String tmp = Integer.toString(i);
System.out.println(tmp);
}
System.out.println("BOOM!");
}
What local variable, I wonder? About the garbage collection process, note that you can never know when exactly will the GC kicks in. The furthest you can go is to suggest that the GC process is being executed.
The other fact about GC is that, JLS mandates nothing about the algorithm being used for GC. So how GC is performed is really vendor-and-platform-dependent.
Cheers!

Originally posted by rob mcfarland:
After a week or so of being a passive Javaranch participant, I've plucked up the courage to actually post something. Following is a question from Exam Cram by Bill Brogden:

When the program reaches line 6, how many of the String objects creatd in line 3 are eligible for garbage collection? (Assume that the System.out object is not keeping a reference.)
a) None
b) 1
c) 10
d) 11
The answer given is c with the explanation that even though the tmp variable is out of scope in line 6, the local variable still has a reference to the last String created.
I've read this about a hundred times and still don't get it. I thought tmp would be out of scope by line 6 as its declared within the for loop, thereby meaning all the Strings created in the for loop (11) would be eligible for garbage collection. If anyone can explain I'd be much appreciated. Also, does anyone know where theres a really solid explanation of the garbage collection process. Every question I see on it seems to rely on the use of words like 'should' as opposed to 'must' with regard to when its activated.
Cheers,
Rob.


 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,
Firstly, can you edit your post and add line numbers to your code?? I just don't want people to 'guess' which is line number 6.
Since this topic has been discussed here a lot of times, I would really like you to read the old posts. I have included the links below to help you get started. Ofcourse, if you are still in doubt after reading all the stuff, I will be glad to help you out
Some old JavaRanch posts on the exact same question -
 
Ray Hsieh
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, thanks Ajith!
That question turned out to be more interesting than I thought!
Cheers
 
rob mcfarland
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajith
Apologies for the lack of line numbering - that was a schoolboy error. Many thanks for the previous thread references and garbage collection explanations. I get the feeling that this particular discussion point is out of scope for the exam.
I promise to use the Search facility in future!!
Cheers
Rob.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above discussion suggests that no garbage collection will take place as long as the loop body is being executed. Does that mean that an infinite loop which is assigning some variable again and again will definitely stop due to an out of memory error at some point of time?
 
Ray Hsieh
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohit,
I don't think so.. Check this snippet out:
for (int i = 10 ; i >= 0; i--) {
B tmp = new B();
System.out.println(tmp);
System.gc();
Thread.sleep(2000);
}
Everytime a loop ends, a reference to the String returned by Integer.toString() goes out of scope... so, theoretically, every loop will make one String eligible for GC.
Yes, I've read the author's explanation, but either way, you keep losing a reference to an object in every loop, so I don't think you'll have OutOfMemoryError anytime soon.

Originally posted by mohit joshi:
The above discussion suggests that no garbage collection will take place as long as the loop body is being executed. Does that mean that an infinite loop which is assigning some variable again and again will definitely stop due to an out of memory error at some point of time?

 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ray,
I want to distinguish between a single run of loop and entire loop ending. If the string is overwritten in subsequent runs of the loop, then when the above program reaches BOOM, it is possible that 10 String objects are already garbage collected and only one remains to be GCed. So both 10 and 11 are wrong answers?
Or will it happen only when you are explicitly suggesting to GC like in your code above,(which doesnt ensure that the GC will be performed anyway).
[This message has been edited by mohit joshi (edited October 06, 2000).]
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohit,
Read the question again -
, how many of the String objects creatd in line 3 are eligible for garbage collection?
You can never tell whether the garbage collector actually runs before the loop completes. As we all know GC is vendor specific and you cannot assume anything about when it is run. That's why all of the questions in the exam on this topic only talks about eligibility for garbage collection.
Hope that helps,
Ajith
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont mean to pick on anything, but there is some ambiguity to this question.The author has gone to lengths to discuss how 10 or 11 may be correct answers by dessecting jvm implementations.
to quote jim yingst:
"From an exam perspective also, I'd say the answer should be 11 simply because Brogden's argument for 10 (and my counterargument for 11) are outside the scope of what you're expected to know for the exam. "
If the question is about theoretical eligibility of how many objects are ready for gc, there is no way to be sure that the answer is not 5,6,7 or 8. Because some objects may have been GCed while the loop was still running. Going through this discussion I almost concluded that there is some specification which says that the GC is done after the for loop ends.
[This message has been edited by mohit joshi (edited October 06, 2000).]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked the errata on the Exam Cram book and Bill Brogden
says "don't take this questions seriously".
The following is an excerpt from a LENGTHY discussion he does on this topic in which he disassembles the code and gets in WAY over my head. I you really want to become an expert on GC, I suggest you start here: http://www.lanw.com/java/localvariables.htm
___________________________________________________________
Some Thoughts On Java 2 Exam Cram, Chapt 8 Question 7
It turns out that this question is a lot more interesting than I thought. The answer seems to depend on how
clever the JVM garbage collection is.
What I was trying to get at with this question is the behavior of the garbage collector with respect to local variables in a method. The question is complicated by the scope of the variable, which is restricted to the for loop.
-Brogden
reply
    Bookmark Topic Watch Topic
  • New Topic