Servlet 中的 JNDI 查找会导致 permgen 内存泄漏吗?

发布于 2024-10-20 15:32:16 字数 1262 浏览 2 评论 0原文

由于 JBoss 4.2 不支持 @EJB 注入,因此我使用 JNDI 查找来引用 Servlet 所需的 EJB。

我担心这种类型的查找可能会导致 JVM 中的 Permgen 非堆内存增长。

据我了解 JNDI,它是动态类加载的一种形式,因此这可能会导致类加载器泄漏。

所以我的问题是,随着时间的推移,下面的 servlet 代码是否可能导致 Permgen 内存泄漏?

另外,我应该在查找后显式调用 InitialContext 上的 close() 方法吗?由于此处(在 Servlet 中)实例化的方式,GC 是否有可能未按预期清理 InitialContext?

谢谢。

public class MyServlet extends HttpServlet {

// JBoss 4.x does not support @EJB injections in servlets (see jndi lookup below)
@EJB
private MyService myService;

private static final String SERVICE_JNDI_NAME = "MyServiceBean";

private Logger log = Logger.getLogger(this.getClass().getPackage().getName());


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {
    // JBoss 4.x does not support @EJB injections in servlets
    InitialContext ctx = new javax.naming.InitialContext();
    myService = (MyService) ctx.lookup(SERVICE_JNDI_NAME);
} catch (NamingException e) {
    log.warn("NamingException trying to lookup MyService in context");
    throw new RuntimeException(e);
}

...

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/page.jsp");
requestDispatcher.forward(request, response);
}

}

Since JBoss 4.2 does not support @EJB injections, I am using a JNDI lookup to reference an EJB that is needed by a Servlet.

I am concerned that this type of lookup may be causing the Permgen non-heap memory in the JVM to grow.

As I understand JNDI, it is a form of dynamic classloading, so this might be causing a classloader leak.

So my question is, could the below servlet code possibly be causing a Permgen memory leak over time?

Also, should I be explicitly calling the close() method on InitialContext after the lookup? Is there a chance that GC is not cleaning up the InitialContexts as expected due to the way they are being instantiated here (in a Servlet)?

Thank you.

public class MyServlet extends HttpServlet {

// JBoss 4.x does not support @EJB injections in servlets (see jndi lookup below)
@EJB
private MyService myService;

private static final String SERVICE_JNDI_NAME = "MyServiceBean";

private Logger log = Logger.getLogger(this.getClass().getPackage().getName());


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {
    // JBoss 4.x does not support @EJB injections in servlets
    InitialContext ctx = new javax.naming.InitialContext();
    myService = (MyService) ctx.lookup(SERVICE_JNDI_NAME);
} catch (NamingException e) {
    log.warn("NamingException trying to lookup MyService in context");
    throw new RuntimeException(e);
}

...

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/page.jsp");
requestDispatcher.forward(request, response);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那一片橙海, 2024-10-27 15:32:16

JNDI是目录查找服务,是Java企业版应用的核心技术服务器。除非您的实现存在错误或应用程序具有不寻常的使用模式,否则我预计由于 JNDI 加载的类最终会稳定下来。

无论哪种方式,我强烈建议您使用堆转储分析器。在应用程序运行时拍摄多个快照,并查看当永久代不断增加时添加的内容。此信息将直接向您显示问题所在,或帮助将根本原因缩小到更小的范围。

JNDI is a directory lookup service and a core technology of Java Enterprise Edition application servers. Unless you have a buggy implementation or have an application with an unusual usage pattern, then I would expect the classes loaded because of JNDI to stabilize eventually.

Either way, I strongly recommend you to use a heap dump analyzer. Take several snapshots while your application is running and see what is added when the permgen keeps increasing. This information will either show you directly what the problem is, or help narrow down the root cause to a much smaller area.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文