title: CDI Application and Dependent scopes can conspire to impact garbage collection?
body: <p>We're starting to experiment with implementing our backend services using CDI. The scenario is this:</p>
  
  <p>EJB with @Startup is started when EAR deployed. An ApplicationScoped bean is injected onto this:</p>
  
  <pre><code>@ApplicationScoped
  public class JobPlatform {
  
      private PooledExecutor threadHolder;
  
      @Inject @Any
      private Instance&lt;Worker&gt; workerSource;
  ...
  </code></pre>
  
  <p>The bean also has an Observer method, which, when an event is observed, gets a worker bean from the Instance workerSource and puts it on the threadPool, where it eventually runs to completion.</p>
  
  <p>All working nicely. However... we've started to see garbage collection issues. A JMAP heap histogram shows that there are many of these workers hanging around, un-garbage collected.</p>
  
  <p>We believe that this is down to the combination of CDI scoping. The API page for @Dependant (<a href="http://docs.jboss.org/cdi/api/1.0-SP1/javax/enterprise/context/Dependent.html" rel="nofollow noreferrer">http://docs.jboss.org/cdi/api/1.0-SP1/javax/enterprise/context/Dependent.html</a>) reinforces more clearly what's in the docs:</p>
  
  <blockquote>
    <ul>
    <li>An instance of a bean with scope @Dependent injected into a field, bean constructor or initializer method is a dependent object of the bean or Java EE component class instance into which it was injected.</li>
    <li>An instance of a bean with scope @Dependent injected into a producer method is a dependent object of the producer method bean instance that is being produced.</li>
    <li>An instance of a bean with scope @Dependent obtained by direct invocation of an Instance is a dependent object of the instance of Instance.</li>
    </ul>
  </blockquote>
  
  <p>So, following this:</p>
  
  <ul>
  <li>The workerSource bean is bound to JobPlatform, and therefore has an ApplicationScoped lifetime</li>
  <li><em>Any worker beans retrieved using that instance are bound to it, and therefore have an ApplicationScoped lifetime</em></li>
  <li>Because the beanstore of the ApplicationScoped context (my knowledge of the terminology gets a bit hazy here) still has a reference to worker beans, they're not destroyed/garbage collected</li>
  </ul>
  
  <p>Does anyone using CDI agree with this? Have you experienced this lack of garbage collection and, if so, can you suggest any workarounds?</p>
  
  <p>The workers cannot be ApplicationScoped, yet the platform has to be. If we were to create a custom WorkerScope (uh ohhh...) and annotate each worker class with it, would that be sufficient to separate the dependency between worker and instance source?</p>
  
  <p>There're also some suggestions at <a href="https://stackoverflow.com/questions/6459449/is-it-possible-to-destroy-a-cdi-scope">Is it possible to destroy a CDI scope?</a> that I will look at, but wanted some backup on whether scoping looks like a valid reason.</p>
  
  <p>Hope you can help, thanks.</p>