I'm working with some code where one object, "foo", is creating another
object, "bar", and passing it a Callable
. After this foo will return
bar, and then I want foo to become unreachable (ie: available for
garbage collection).
My initial thought was to just create the Callable
anonymously. eg:
class Foo {
...
public Bar createBar() {
final int arg1 = ...
final int arg2 = ...
final int arg3 = ...
return new Callable<Baz>() {
@Override
public Baz call() {
return new Baz(arg1, arg2, arg3);
}
};
}
}
It occurred to me that this might not actually work as desired, however,
as an inner class typically keeps a reference to its enclosing object.
I don't want a reference to the enclosing class here, because I want the enclosing object to be
collected while the Callable
is still reachable.
On the other hand, detecting that the enclosing instance is never actually referred to should be pretty trivial, so perhaps the Java compiler is smart enough to not include a reference in that case.
So... will an instance of an anonymous inner class hold on to a reference to its enclosing instance even if it never actually uses the enclosing instance reference?
The static alternative (in this case) is not much larger (1 line):
BTW: if you use a Lambda in Java8 there will be no nested class generated. However I am not sure if the
CallSite
objects which get passed around in that case hold an reference to the outer instance (if not needed).Yes, instances of anonymous inner classes hold on to a reference to their enclosing instances even if these references are never actually used. This code:
When compiled with
javac
generates two class files,Outer.class
andOuter$1.class
. Disassembling the latter, the anonymous inner class, withjavap -c
yields:The
putfield
line shows that a reference to the enclosing instance is being stored in the fieldthis$0
(of typeOuter
) by the constructor even though this field is never used again.This is unfortunate if you're attempting to create small potentially long-lived objects with anonymous inner classes as they'll hold onto the (potentially large) enclosing instance. A workaround is to use an instance of a static class (or a top-level class) instead. This is unfortunately more verbose.
You can easily turn a nested anonymous-class into a "static" anonymous-class by introducing a static method in your class.