我在 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)