FOP Servlet内存泄漏

huangapple go评论74阅读模式
英文:

FOP Servlet memory leak

问题

我已创建一个Web Servlet,示例如下:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());

        // 设置缓冲区以获取内容长度
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(PATH_TO_XSL));
        // 确保XSL转换的结果通过FOP进行传输
        Result res = new SAXResult(fop.getDefaultHandler());

        // 设置输入源
        Source src = new StreamSource(new File("./resources/Employees.xml"));

        // 启动转换和呈现过程
        transformer.transform(src, res);

        // 准备响应
        response.setContentType("application/pdf");
        response.setContentLength(out.size());

        // 将内容发送到浏览器
        response.getOutputStream().write(out.toByteArray());
        response.getOutputStream().flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然而,当我停止Tomcat时,我遇到了内存泄漏问题。我在日志中找到的信息如下:

SEVERE [http-nio-8080-exec-4] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks Web应用程序[fop]创建了一个类型为[java.lang.ThreadLocal](值为[java.lang.ThreadLocal@294d3ff4])的ThreadLocal,值为[org.apache.xerces.parsers.SAXParser](值为[org.apache.xerces.parsers.SAXParser@af83efc]),但在停止Web应用程序时未能删除它。线程将在一段时间后重新创建,以尝试避免可能的内存泄漏。

我尝试过的方法:

ServletContextcontextDestroyed方法中强制使用 System.gc();

有没有办法解决这个问题?内存泄漏发生在我停止Tomcat之后。

英文:

I have created a web servlet as the following example:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());

        //Setup a buffer to obtain the content length
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(PATH_TO_XSL));
        //Make sure the XSL transformation's result is piped through to FOP
        Result res = new SAXResult(fop.getDefaultHandler());

        //Setup input
        Source src = new StreamSource(new File("./resources/Employees.xml"));

        //Start the transformation and rendering process
        transformer.transform(src, res);

        //Prepare response
        response.setContentType("application/pdf");
        response.setContentLength(out.size());

        //Send content to Browser
        response.getOutputStream().write(out.toByteArray());
        response.getOutputStream().flush();
    }catch(Exception e){
        e.printStackTrace();
    }
}

Howerver, when I stop the tomcat I get a memory leak. What I could find in the logs:

    SEVERE [http-nio-8080-exec-4] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [fop] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lan
g.ThreadLocal@294d3ff4]) and a value of type [org.apache.xerces.parsers.SAXParser] (value [org.apache.xerces.parsers.SAXParser@af83efc]) but failed to remove it when the web application was stopped. Threads are going to be renewed over t
ime to try and avoid a probable memory leak.

What I have tried:

Force System.gc(); in contextDestroyed method of ServletContext.

Any ideas how I can fix this problem? The memory leak is occurring after I stop tomcat.

答案1

得分: 0

使用Maven中的jiderhamn jars解决。

英文:

solved using jiderhamn jars from maven.

huangapple
  • 本文由 发表于 2020年8月5日 20:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定