我在Tomcat(9.0.8)中部署了一个Web应用程序,我在contextDestroyed()中取消注册JDBC驱动程序以避免内存泄漏 . 我也在使用log4j2版本2.11.0 . 每次重新部署/取消部署或关闭时,我都会收到与log4j2相关的内存泄漏警告消息,如下所示:

WARNING [http-nio-8082-exec-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [xxxxx] appears to have started a thread 
named [Log4j2-TF-6-Scheduled-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 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.getTask(ThreadPoolExecutor.java:1074)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 java.lang.Thread.run(Thread.java:748)

因此,根据here给出的解决方案,我添加了log4j-web版本2.11.0依赖项 . 现在log4j2内存泄漏错误消失了但是我在listner 's contextDestroy() don'中使用带有info / debug的Logger实例的消息被打印出来了 . System.out.println()工作正常 .

那么为什么我在使用依赖log4j-web时无法在contextDestroyed()中使用Log4j2但是如果我删除了depdency(然后我得到内存泄漏问题)就可以使用它?

操作系统是Ubuntu 18.04 LTS . Tomcat 9.0.8,Java 1.8