Log4j2内存泄漏 [英] Log4j2 memory leak
问题描述
我到处都在寻找解决方案,现在我开始怀疑这是否真的是一个问题.
I've searched everywhere for a solution to this, and now I'm beginning to wonder whether it's really an issue.
我正在将log4j2
作为记录程序引入我的应用程序中,当我这样做时,在Tomcat服务器8.5.24的重新加载,取消部署或停止时,会留下内存泄漏,这仅在我引入了记录代码.
I'm introducing log4j2
as the logger into my application and when I do that, on a reload, undeploy or stop of the Tomcat server 8.5.24, a memory leak is left, which only happens once I introduce the logger into the code.
我在pom.xml
中的依赖项:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
我的WebServlet
:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "Servlet", urlPatterns = {"/servlet"}, loadOnStartup = 1)
public class Servlet extends HttpServlet {
private final Logger logger = LogManager.getLogger(Servlet.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("doGet");
PrintWriter out = response.getWriter();
out.print("Servlet loaded");
out.flush();
out.close();
}
}
我的log4j2.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d %p %c %m%n" />
</Console>
<File name="File" bufferedIO="true" fileName="logs/log4j2-file-sync-${date:HH:MM:ss.SSS}.log">
<PatternLayout pattern="%d %p %c %m%n" />
</File>
</Appenders>
<Loggers>
<Root level="all" includeLocation="false">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
这是我得到的错误:
The following web applications were stopped (reloaded, undeployed), but their
classes from previous runs are still loaded in memory, thus causing a memory
leak (use a profiler to confirm):
/myapp
推荐答案
要感谢@RemkoPopma的意见和Tomcat JIRA董事会的@RalphGoers的建议,以回答我自己的问题.
To answer my own question thanks to the advice of @RemkoPopma from the comments and @RalphGoers from the Tomcat JIRA board.
我只需要在我的pom.xml
中具有以下依赖关系,该依赖关系也将导入core
和api
并处理Log4J2的正常关闭.
I only needed the following dependency in my pom.xml
which also imports the core
and api
and which handles proper shutdown of the Log4J2.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.10.0</version>
</dependency>
可以找到有关此依赖关系的特定文档
The specific documentation for this dependency can be found
https://logging.apache.org/log4j/2 .x/manual/webapp.html
这篇关于Log4j2内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!