I noticed while reading this article on Java's garbage collection this note:
NOTE: The im object is set to null because there is no gurantee the stack slot occupied by it will be cleared when it goes out of scope. A later method invocation whose stack frame contains the slot previously occupied by im might not put a new value there, in which case, the garbage collector still considers the slot to contain a root... As a precaution, you can increase the probability that an object will become softly, weakly, finalizable, or phantomly reachable by clearing variables that refer to it.
So Java doesn't even guarantee when the scope of a method is exited that local variables are actually available to garbage collect? This seems really broken to me. That article is from 1998 -- does Java still work this way?
Update: I asked our Java expert at work and he said that, for one, Sun's VM never worked this way, but some other vendors' did for efficiency (essentially they'd just move the stack pointer without telling the garbage collector about it); that's the only reason the article mentioned it. He also said other vendors' VMs don't work this way anymore, so this should no longer be an issue in Java at all.
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):