1 2 Previous Next 15 Replies Latest reply on May 13, 2016 11:22 AM by Chris Rankin

    Memory leak - WELD's optimisation is not being applied.

    Chris Rankin Novice



      I've recently discovered a memory leak in my application (sample project is attached). Now I am aware that when Instance<>.get() creates a @Dependent scoped bean, the CDI spec states that the bean "belongs" to the Instance<> and should therefore be destroyed afterwards using Instance<>.destroy(). However, [WELD-1076] implemented an optimisation whereby the Instance<> would not keep a contextual reference for the bean provided the bean did not implement a @PreDestroy method or a @Disposes method, or was itself dependent on a @Dependent bean with such a requirement.


      In my example application, I have two @Dependent beans called Messenger and Ping. Neither bean has a @PreDestroy method. However, Messenger has a dependency on Instance<Ping>.

      • Every GET /ping request invokes Instance<Ping>.get(), and the Instance correctly does not keep a contextual reference for the Ping bean .
      • Every GET /leak request invokes Instance<Messenger>.get(). However, in this case the Instance does keep a contextual reference for the Messenger bean .

      So Messenger's Instance<Ping> dependency is preventing WELD from optimising away the contextual reference for each Messenger bean, despite the fact that Instance<Ping> will never contain any contextual references for Ping beans that will need destroying.


      I would argue that if WELD can apply its optimisation to a bean then it can also apply that optimisation to Instances of that bean. Does this sound reasonable, please?




        1 2 Previous Next