简体   繁体   English

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

[英]Can a JNDI lookup in a Servlet cause a permgen memory leak?

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. 由于JBoss 4.2不支持@EJB注入,因此我使用JNDI查找来引用Servlet所需的EJB。

I am concerned that this type of lookup may be causing the Permgen non-heap memory in the JVM to grow. 我担心这种类型的查找可能导致JVM中的Permgen非堆内存增加。

As I understand JNDI, it is a form of dynamic classloading, so this might be causing a classloader leak. 据我了解,JNDI是动态类加载的一种形式,因此这可能会导致类加载器泄漏。

So my question is, could the below servlet code possibly be causing a Permgen memory leak over time? 所以我的问题是,下面的servlet代码是否可能随着时间的推移导致Permgen内存泄漏?

Also, should I be explicitly calling the close() method on InitialContext after the lookup? 另外,查找后是否应该在InitialContext上显式调用close()方法? 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)? 由于在此处(在Servlet中)实例化InitialContext的方式,GC是否有可能无法按预期清理InitialContext?

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);
}

} }

JNDI is a directory lookup service and a core technology of Java Enterprise Edition application servers. JNDI是目录查找服务,是Java Enterprise Edition应用程序服务器的核心技术。 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. 除非您有错误的实现或具有异常使用模式的应用程序,否则我希望最终由于JNDI而加载的类能够稳定下来。

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. 在应用程序运行时拍摄多个快照,并查看当permgen不断增加时添加的内容。 This information will either show you directly what the problem is, or help narrow down the root cause to a much smaller area. 此信息将直接向您显示问题所在,或帮助将根本原因缩小到更小的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM