内存泄漏/ContextInjectionFactory/IEclipseContext

Memory Leak / ContextInjectionFactory / IEclipseContext

我在 Eclipse RCP 应用程序中使用依赖注入 (DI)。我有很多 classes 执行类似于下面的代码:

public class SomeClass {
    @Inject
    private IEclipseContext context;

    private SomeObject void someMethod(){
        SomeObject someObject =
            ContextInjectionFactory.make(SomeObject.class, context);
        // Do stuff with someObject
    }
 }

当我使用 jvisualvm 监视应用程序时,我注意到存在内存泄漏。 EclipseContext 对象不断增长,直到最终耗尽内存。

如果我执行以下操作,内存泄漏就会消失:

public class SomeClass {
    @Inject
    private IEclipseContext context;

    private SomeObject void someMethod(){
        IEclipseContext childContext = context.createChild();
        SomeObject someObject =
            ContextInjectionFactory.make(SomeObject.class, childContext);
        childContext.dispose();
        // Do stuff with someObject
    }
 }

我没有看到任何支持我的解决方法的文档。在创建 class 之后处理 childContext 是否有任何负面影响?使用 CIF 时是否有我没有遇到过的更好的总体方法?

就其价值而言,我的代码有很多 classes,其中一些用 @Singleton / @Creatable 注释。我不确定这些是否会受到处置父上下文的影响。

谢谢!

当您像这样使用注入在 class 中设置字段时:

public class Test
{
  @Inject
  private StatusReporter rep;
  @Inject
  private IEventBroker broker;


  public Test()
  {
  }
}

Eclipse 必须跟踪已注入的每个字段,以便它可以在 Eclipse 上下文中的值更改时重新注入该字段。这涉及为每个注入字段创建 TrackableComputationEx‌​t‌​ContextInjectionList‌​ener 对象。

如果您改为像这样在构造函数中注入值:

public class Test
{
  private StatusReporter rep;
  private IEventBroker broker;

  @Inject
  public Test(StatusReporter rep, IEventBroker broker)
  {
    this.rep = rep;
    this.broker = broker;
  }
}

Eclipse 不需要跟踪构造函数注入,因此不会创建这些对象(但如果上下文值发生更改,您也不会获得任何更新)。

对此进行测试,似乎仍然创建了一个内部使用跟踪对象。