-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Description
Perhaps this behavior is known but I couldn't find any mention of it. This code...
public static void main(final String[] args) {
Injector parent = Guice.createInjector();
parent.createChildInjector(createModule(new GiantObject("notleaked")));
// GiantObject("notleaked") can be garbage collected.
parent.createChildInjector(createTypeLiteralModule(new GiantObject("leaked")));
// GiantObject("leaked") cannot be garbage collected.
}
private static AbstractModule createModule(final GiantObject o) {
return new AbstractModule() {
@Override
protected void configure() {
bind(GiantObject.class).toInstance(o);
}
};
}
private static AbstractModule createTypeLiteralModule(final GiantObject o) {
return new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<GiantObject>() {
}).toInstance(o);
}
};
}...puts a reference to the TypeLiteral in the parent Injector, and appears never to be garbage collected. JProfiler lists InheritingState/WeakKeySet in the first GC root. This leaks whatever is in scope for the TypeLiteral (i.e. the GiantObject("leaked") instance).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels