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