I read Java Platform Performance (unfortunately, the link seems to have disappeared from the Internet since I originally asked this question) and section A.3.3 bothers me.
I worked on the assumption that a variable that fell out of scope would no longer be considered a GC root, but this article seems to contradict this.
Do recent JVMs, in particular Sun version 1.6.0_07, still have this limitation? If so, then I have a lot of code to analyze ...
I ask a question because the article is from 1999 - sometimes things change, especially in the GC world.
Since the document is no longer available, I would like to rephrase the problem. The document implied that the variables that were defined inside the method would be considered the root of the GC until the method completed, and only until the block of code ended. Therefore, setting the variable to null was necessary so that the referenced object was garbage collected.
This meant that a local variable defined in a conditional block in the main () method (or a similar method containing an infinite loop) would cause a one-time memory leak if you did not change the variable just before it dropped out of scope.
The code from the selected answer illustrates the problem well. In the JVM version specified in the document, the foo object cannot be garbage collected when it falls out of scope at the end of the try block. Instead, the JVM will hold the link until the end of the main () method, even if it is not possible to use that link.
This, apparently, is the source of the idea that nulling the variable reference would help the garbage collector, even if the variable was supposed to go out of scope.
java garbage-collection memory-leaks jvm sun
Bill Michell Nov 07 '08 at 9:25 2008-11-07 09:25
source share