2 Replies Latest reply on Feb 27, 2013 4:00 AM by Max Pimm

    Garbage collection of Weld proxies in Stateless session bean

    Max Pimm Newbie

      I am profiling our application that is running on JBoss AS7 with EJB and CDI.


      We are seeing an acumulation of Weld$Proxy instances for a particular class that are not being garbage collected.


      The scenario is:


      1) We have a bean, Service1, annotated with @Stateless, of which we can see 20 instances being pooled by the app server.

      2) This bean has a member, service2, that is dynamically injected by a @Producer method (in another class) via Instance<Service2> using service2.get().

      3) The producer method has no scope annotation and accepts two @Stateless beans as parameters and returns one of them according to some runtime checks.


      What seems to be happenning is that each time the producer method is called a Weld$Proxy object is instantiated. I imagine that this is necessary since the producer returns a @Stateless (in other cases where is returns pojo beans no proxy is created).


      Upto here i have no complaints.


      However it would seem that each time the producer is called the proxy returned is added to an list of proxies referenced by Service1. This list seems only to grow and since Service1 hangs around for the lifetime of the application is never garbage collected.


      Looking at the reference graph in our profiler we see something like

           Service1 service1

                > InstanceImp creationalContext

                     > CreationalContextImpl dependentInstances

                          > Collections$SyncronizedRandomAccessList list

                               > Second element in list > SerializableContextualInstance instance


      This instance is our Weld$Proxy.


      I Hope that we have missed something obvious and that there is some annotation that will come to our rescue.