小编Jam*_*mes的帖子

CDI | 应用程序/从属范围| 内存泄漏 - javax.enterprise.inject.Instance<T> 未收集垃圾

我在 TomEE Java 应用程序中使用 Instance 作为惰性/动态注入器,并且我注意到我的应用程序中存在内存泄漏。这对我来说是第一次,所以看到 Java EE 库中概述的内存泄漏警告实际上令人惊讶:

package javax.enterprise.inject;

public interface Instance<T> extends Iterable<T>, Provider<T>
{
    /**
     * Destroy the given Contextual Instance.
     * This is especially intended for {@link javax.enterprise.context.Dependent} scoped beans
     * which might otherwise create mem leaks.
     * @param instance
     */
    public void destroy(T instance);
}
Run Code Online (Sandbox Code Playgroud)

现在这很可能是由与@ApplicationScoped和 的冲突引起的Instance<T>。我已经提供了一个示例,说明层在我的类中的位置。注意嵌套的Instance<T>. 这是为了提供任务的动态注入。

外层

@ApplicationScoped
public class MessageListenerImpl implements MessageListener {

    @Resource(name="example.mes")
    private ManagedExecutorService mes;

    @Inject @Any
    private Instance<Worker<ExampleObject>> workerInstance;

    // ...

    @Override …
Run Code Online (Sandbox Code Playgroud)

garbage-collection memory-leaks cdi apache-tomee jakarta-ee

5
推荐指数
1
解决办法
1666
查看次数