[cdi-dev] Are we always able to invoke @PreDestroy for @Dependent beans?

Marius Bogoevici mbogoevi at redhat.com
Thu Mar 8 09:51:41 EST 2012


So what if UserHelper would get destroyed at the end of the invocation and the only long-term deps that get stored in the CC would be the ones defined by injectors? It seems that the intent for UserHelper is to be depended upon by the observer invocation, not by the bean.

Sent from my iPad

On 2012-03-08, at 9:08, Pete Muir <pmuir at redhat.com> wrote:

> I think we have to offer both options. One which in which @PreDestroy is reliably invoked but with memory impact, other in which it's not reliable.
> 
> Which one should be the default?
> 
> On 8 Mar 2012, at 12:47, Mark Struberg wrote:
> 
>> My answer sadly is no.
>> 
>> Dependent objects are stored in the CreationalContext which is held in the context which holds the outer NormalScoped contextual instance.
>> If the outer NormalScoped contextual instance gets desroyed, we also properly destroy all @Dependent beans in the CreationalContext.
>> 
>> So far so good, but now let's consider hte following scenario:
>> 
>> 
>> @ApplicationScoped
>> public class MyService {
>> 
>>  void doStats(@Observes RequestStatistics, UserHelper uh) {
>> 
>> UserHelper is @Dependent and a new instance will get created each time doStats() gets called. 
>> 
>> What happens is that we don't know whether the UserHelper internally gets stored or just thrown away. In our sample most probably the later.
>> Each UserHelper will get stored in the CreationalContext of our single MyService instance. After 500.000 requests, our CreationalContext would contain 500.000 UserHelpers. Not smart, eh? This would just create a fat memory leak...
>> 
>> 
>> What we can do is to store only WeakReferences. E.g. via a WeakHashmap. This will automatically drop the entries if the instances in it get garbage collected. But in this case are not able to invoke @PreDestroy anymore I fear, because gone is gone ...
>> 
>> LieGrue,
>> strub
>> 
>> 
>> _______________________________________________
>> cdi-dev mailing list
>> cdi-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/cdi-dev
> 
> 
> _______________________________________________
> cdi-dev mailing list
> cdi-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/cdi-dev



More information about the cdi-dev mailing list