Do inner classes cause a memory leak?
John Rose
john.r.rose at oracle.com
Fri Jul 16 18:39:40 PDT 2010
On Jul 16, 2010, at 12:18 PM, Reinier Zwitserloot wrote:
> the way the JLS currently specifies how anonymous inner classes
> work means they can NOT be static, and that a reference to the outer
> instance is passed into the inner instance even if it isn't actually
> necessary.
The JLS says that the reference to the enclosing instance is passed to the constructor. I don't think it implies anywhere that this value has to be *stored* in a *field* (i.e, "this$N"). If the field is not used, it should not be generated, though I suppose compilers don't always do this optimization.
Any storage leaks due to needless up-level links is an issue of implementation quality (of javac), not an issue of architecture. That was the original intention. Closures (in any GC language) are vulnerable to the same sorts of quality issues.
-- John
More information about the lambda-dev
mailing list