簡體   English   中英

log4j2線程的Tomcat內存泄漏問題

[英]Tomcat memory leak issue of log4j2 thread

我正在使用log4j2進行日志記錄,tomcat8和java8版本。 我使用屬性“ monitorInterval”來定期檢查我的log4j2.xml。 在關機期間,我的tomcat遇到內存泄漏問題。如何解決此內存泄漏問題?

以下是Catalina日志:

2016年10月6日15:13:55.927警告[localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads Web應用程序[mywebapp]似乎已啟動名為[Log4j2-Log4j2Scheduled-1]的線程,但未能阻止它。 這很可能造成內存泄漏。 線程的堆棧跟蹤:sun.misc.Unsafe.park(本機方法)java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)java.util.concurrent.locks.AbstractQueuedSynchronizer $ ConditionObject.awaitNanos(AbstractQueuedSynchronizer。 java:2078)java.util.concurrent.ScheduledThreadPoolExecutor $ DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)java.util.concurrent.ScheduledThreadPoolExecutor $ DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)java.util.concurrent.ThreadPoolExecutor ThreadPoolExecutor.java:1067)java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)java.lang.Thread.run(Thread。 Java的:745)

提前致謝。

更新:我分析了日志,實際上Log4jServletContextListener銷毀后,記錄器上下文又重新啟動。

2016-10-22 13:49:36,347 localhost-startStop-2調試Log4jServletContextListener確保Log4j正確關閉。 2016-10-22 13:49:36,382 localhost-startStop-2調試啟動LoggerContext [name = bb4719,org.apache.logging.log4j.core.LoggerContext@d77214] ...

實際上,在我的應用程序中,我在web.xml中使用spring ContextLoaderListner,因此在銷毀spring listner時可能會在內部使用日志記錄。

謝謝

它應該工作。

確保在構建中包括log4j-web

例如作為Maven依賴。

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
</dependency>

如果您使用的是Servlet 3.0或更高版本的容器(例如在Tomcat 8中),則不需要其他配置(只要您沒有忽略Tomcat在某些jar中掃描ServletContainerInitializer即可)。 有關更多信息,請參見在Web應用程序中使用Log4j 2


UPDATE

我已經用您的設置(Tomcat 8.0.38,Log4j-2.6.2)對其進行了設置,並且可以正常工作。 要檢查它是否Log4jServletContextListenerLog4jServletFilter被初始化的設置StatusLogger水平DEBUG在你的log4j2.xml

<Configuration monitorInterval="30" status="DEBUG">

之后,部署應用程序后,您應該能夠在根記錄器附加程序中看到以下輸出。

2016-10-14 20:21:36,762 RMI TCP Connection(2)-127.0.0.1 DEBUG Log4jServletContextListener ensuring that Log4j starts up properly.
2016-10-14 20:21:36,764 RMI TCP Connection(2)-127.0.0.1 DEBUG Log4jServletFilter initialized. 

如果您的應用程序被重新部署,您應該在日志中看到以下幾行。

2016-10-14 20:22:00,276 RMI TCP Connection(2)-127.0.0.1 DEBUG Log4jServletFilter destroyed.
2016-10-14 20:22:00,286 RMI TCP Connection(2)-127.0.0.1 DEBUG Log4jServletContextListener ensuring that Log4j shuts down properly.

如果您沒有看到日志。 如果jarsToSkip包含任何jar4的log4j2,或者您在web.xml中定義了isLog4jAutoInitializationDisabled參數且值為false ,則應檢查catalina.properties。

<context-param>
   <param-name>isLog4jAutoInitializationDisabled</param-name>
   <param-value>false</param-value>
</context-param> 

log4j-web包含一個web-fragment.xml

<web-fragment xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                  http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"
              version="3.0" metadata-complete="true">
    <!-- The Log4j web fragment must be loaded before all other fragments. The configuration below should make this
        happen automatically. If you experience problems, try specifying an <absolute-ordering> in your web.xml
        deployment descriptor. -->
    <name>log4j</name>
    <distributable />
    <ordering>
        <before>
            <others />
        </before>
    </ordering>
</web-fragment>

如果webapp包含多個片段,則首先加載此片段很重要。 您可以在web.xml配置:

<absolute-ordering>
    <name>log4j</name>
    <others/>
</absolute-ordering>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM