java - FOP Servlet 内存泄漏
问题描述
我创建了一个 web servlet,如下例所示:
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();
}
}
但是,当我停止 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.
我试过的:
方法中System.gc();
的力。contextDestroyed
ServletContext
有什么想法可以解决这个问题吗?停止tomcat后发生内存泄漏。
解决方案
使用来自 maven 的 jiderhamn jars 解决。
推荐阅读
- laravel - 即使在 {{ csrf_field() }}
- angular - Meteor 不呈现 html 模板,而只呈现路径
- html - 通过 css 限制 Internet Explorer 中的行数
- javascript - React setState:多个回调是否同步?
- ios - 如何使用 iOS-Charts 创建具有多个 labelTextColor YAxis 的 LineChartView
- android - 如何使用 Kotlin 在 Android 上的 Apollo GraphQL 中创建 JSON CustomTypeAdapter
- azure - Azure Blob 存储 SDK:关闭日志记录
- python - 如何从路线中找到一个位置的最短距离(Python),
- perl - perl 使用评估修饰符在字符串中增加数字
- continuous-integration - 来自 Go CI/CD 的 Gitlab 连接