-
1. Re: MemoryLeak in javax.inject.Provider chain?
mkouba Apr 18, 2014 3:36 AM (in response to frolovmx)1 of 1 people found this helpfulHi Maxim,
I think this is correct per the spec. The bean A has scope @Dependent. Thus every invocation of Instance.get() returns a new bean instance which depends on the bean Main. Since this bean has scope @Singleton it looks like a memory leak. See also 6.4.1. Dependent objects and 5.6.1. The Instance interface. I don't think that bean A without Provider could solve anything. The key is the "@Inject private Provider<A>" injection point. Also I would avoid using @Singleton in CDI - although supported, it's not defined as a built-in scope, nor the bean defining annotation (CDI 1.1).
-
2. Re: MemoryLeak in javax.inject.Provider chain?
jharting Apr 18, 2014 3:42 AM (in response to mkouba)1 of 1 people found this helpfulWhen you inject Provider<A> you are actually using the Instance<A> built-in bean (Provider is its superinterface). An object obtained using Instance remains managed until the component, into which Instance is injected, is destroyed or until the application explicitly destroys the obtained object. See the following method for details: http://docs.jboss.org/cdi/api/1.1/javax/enterprise/inject/Instance.html#destroy(T)
-
3. Re: MemoryLeak in javax.inject.Provider chain?
frolovmx Apr 19, 2014 7:51 AM (in response to mkouba)I don't think that bean A without Provider could solve anything. The key is the "@Inject private Provider<A>" injection point.
But this is just a fact which makes me doubtfully!
Keeping Main.java and B.java unchanged, I have tried the following again:
case 1) memory leak: instances of a increase
public class A {
@Inject Provider<B> b;
}
case 2) no memory leak
public class A { // no memory leak: instance of a are garbage collected
@Inject B b;
}
case 3) no memory leak
public class A { // no memory leak: instance of a are garbage collected
// no memory leak
}
The memory leak occurs only in 1). case, where A injects Provider of B.
Why in cases 2) and 3) the managed instances of A get destroeyd after excution of garbage collector?
-
4. Re: MemoryLeak in javax.inject.Provider chain?
frolovmx Apr 19, 2014 10:27 AM (in response to mkouba)I made interesting observation: adding @PreDestroy method to Bean A prevents instances of A from beeing destroyed by garbage collector:
case 2a) No memory leak
public class A {
@Inject B b;
}
case 2b) memory leak
public class A {
@Inject B b;
@PreDestroy
public void preDestroy() {}
}
Why does @PreDestroy method make difference?
-
5. Re: MemoryLeak in javax.inject.Provider chain?
frolovmx Apr 19, 2014 11:59 AM (in response to frolovmx)This likely works as expected:
https://issues.jboss.org/browse/WELD-1076
WELD-1076 Optimization - do not keep a reference to dependencies that do not need @PreDestroy / disposer method invocation