Утечка памяти сервлета FOP - PullRequest
       85

Утечка памяти сервлета FOP

0 голосов
/ 05 августа 2020

Я создал веб-сервлет в следующем примере:

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, когда я останавливаю tomcat, я получаю утечку памяти. Что я смог найти в журналах:

    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.

Что я пробовал:

Force System.gc(); in contextDestroyed метод ServletContext.

Любые идеи как я могу решить эту проблему? Утечка памяти происходит после того, как я остановил tomcat.

1 Ответ

0 голосов
/ 28 августа 2020

решено с использованием jiderhamn jars от maven.

...