[cdi-dev] [JBoss JIRA] (CDI-139) Support for unmanaged instances

Mark Struberg (JIRA) jira-events at lists.jboss.org
Wed Mar 7 16:13:38 EST 2012

    [ https://issues.jboss.org/browse/CDI-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12674902#comment-12674902 ] 

Mark Struberg commented on CDI-139:

Just a short notice:
I think this should get added to the spec that way we discussed: automatically destroying all @Dependent contextual instances created via the Instance<T>.get() method when the containing bean will be destroyes. 

This will of course have a few side effects where this might be too aggressive:
Consider the following code:
public class X {
  private @Inject Instance<Y> yProvider;
  private static Y y;

  void doInit() {
    if (y == null) y = yProvider.select().get();

In this case we would 'destroy' y (calling @PreDestroy) at the end of the Request, even if the static y member is still valid.
The same might happen if you store y somewhere else via classic java setters...
> Support for unmanaged instances
> -------------------------------
>                 Key: CDI-139
>                 URL: https://issues.jboss.org/browse/CDI-139
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Beans
>    Affects Versions: 1.0
>            Reporter: Joshua Davis
>             Fix For: 1.1 (Proposed)
> Allow the creation of unmanaged instances.   The CDI context will not keep track of these instances and the application will be responsible for cleaning them up.   This is a fairly typical usage of other DI frameworks such as Guice and PicoContainer.
> Currently, if an ApplicationScoped object injects an {{Instance<T>}} interface, CDI will manage all instances returned by the {{get()}} method as dependents of the application scoped object.   Those instances will be kept in memory by the CDI implementation and will only be GC'd when the application scoped object is destroyed (at the end of the application).   This may look like a memory leak to the user (see WELD-920).
> From P. Muir on WELD-920
> {quote}
> We can describe instances which are attached (as the CDI 1.0 spec requires) as "managed" instances, and those which the user takes responsibility for cleaning up themselves as "unmanaged" instances. In CDI 1.1 I would like to add support for unmanaged instances (the impl will just hand these over and forget about them) and also to allow the app to request an unmanaged instance is cleaned up. Please can someone file a CDI issue for this?
> Weld could certainly be more friendly and more proactively discard instances. Ideas:
> 1) Analyse the dependent instance graph, and if there are no @PreDestroy/@Disposer callbacks on in the graph, do not store the dependent instance for cleanup (this would be a good general optimization
> (2) Add a config option to allow instances created from Instance to be held only as long as the app holds a reference, and if the app doesn't hold a reference for it's lifetime, then Weld would not do any cleanup (Weld would hold a weak ref).
> {quote}

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.jboss.org/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the cdi-dev mailing list