2011-03-13 65 views
44

我配置了tomcat以使用不同的外部開源。這是否很可能在Tomcat中造成內存泄漏?

然而,Tomcat是運行了幾分鐘後,我得到:

重度:Web應用程序創建[MyProject的/]一個ThreadLocal類型[java.lang.ThreadLocal中]的 鍵(值 [[email protected]])以及類型 [org.apache.axis.MessageContext](值爲 [[email protected]])的值,但未能在 web應用程序已停止。這很可能會造成內存泄漏。

是什麼原因造成的?

我要去哪裏看?它可能是在Tomcat上的數據池嗎?

這是什麼意思在Tomcat線程?

EDITED

這是我的全部跟蹤。當應用程序仍在運行時,應用程序似乎會重新加載它的上下文 - 我不知道爲什麼!

Mar 13, 2011 10:56:12 PM org.apache.catalina.core.StandardContext reload 
INFO: Reloading this Context has started 
Mar 13, 2011 10:56:12 PM org.apache.catalina.core.StandardWrapper unload 
INFO: Waiting for 1 instance(s) to be deallocated 
Mar 13, 2011 10:56:13 PM org.apache.catalina.core.StandardWrapper unload 
INFO: Waiting for 1 instance(s) to be deallocated 
Mar 13, 2011 10:56:14 PM org.apache.catalina.core.StandardWrapper unload 
INFO: Waiting for 1 instance(s) to be deallocated 
Mar 13, 2011 10:56:14 PM org.apache.catalina.core.ApplicationContext log 
INFO: Closing Spring root WebApplicationContext 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
SEVERE: The web application [/MyProject] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
SEVERE: The web application [/MyProject] registered the JBDC driver [oracle.jdbc.driver.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [NioSocketAcceptor-1] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [NioProcessor-1] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [NioProcessor-4] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [bitronix-disk-force-batcher] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [bitronix-scheduler] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] is still processing a request that has yet to finish. This is very likely to create a memory leak. You can control the time allowed for requests to finish by using the unloadDelay attribute of the standard Context implementation. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [NioProcessor-7] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/MyProject] appears to have started a thread named [NioProcessor-2] but has failed to stop it. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [[email protected]]) and a value of type [org.mvel2.debug.DebuggerContext] (value [[email protected]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [org.apache.axis.utils.XMLUtils.ThreadLocalDocumentBuilder] (value [[email protected]]) and a value of type [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl] (value [[email protected]cfa]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [null] (value [[email protected]]) and a value of type [java.util.HashMap] (value [{com.sun.faces.patternCache={ = }}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [[email protected]]) and a value of type [org.apache.axis.MessageContext] (value [[email protected]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [org.apache.axis.utils.XMLUtils.ThreadLocalDocumentBuilder] (value [[email protected]]) and a value of type [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl] (value [[email protected]84]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [org.springframework.core.NamedThreadLocal] (value [Transactional resources]) and a value of type [java.util.HashMap] (value [{[email protected][email protected]}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
Mar 13, 2011 10:56:15 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 
SEVERE: The web application [/MyProject] created a ThreadLocal with key of type [null] (value [[email protected]]) and a value of type [com.sun.faces.application.ApplicationAssociate] (value [[email protected]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 
2011-03-13 22:57:27,734 ERROR (   ContextLoader.java:220)  - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext-hibernate.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext-hibernate.xml]: Invocation of init method failed; nested exception is java.lang.OutOfMemoryError: Java heap space 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325) 

回答

23

的消息實際上是相當清楚的:一些與org.apache.axis.MessageContext類型的值形成ThreadLocal - 這是一個很大的提示。這很可能意味着Apache Axis框架本身忘記/無法清理。例如在Logback中發生了同樣的problem。你不應該打擾太多,但向Axis團隊報告錯誤可能是一個好主意。

Tomcat報告此錯誤,因爲每個HTTP工作線程都創建了ThreadLocal。您的應用程序已取消部署,但仍保留HTTP線程 - 並且這些也是。這可能會導致內存泄漏(org.apache.axis.MessageContext無法卸載)以及將來這些線程被重用時的一些問題。

有關詳細信息,請參閱:http://wiki.apache.org/tomcat/MemoryLeakProtection

+0

感謝我編輯我的問題與全部跟蹤 - 我仍然不知道爲什麼會啓動tomcat重新加載在中間的情況下,因此春季的應用程序上下文失敗 – Dejell 2011-03-13 21:41:26

+2

是您遇到意外的上下文重新加載?很少有人[用掙扎](http://forum.springsource.org/showthread.php?t=82423)。至於內存泄漏,它由Axis生成,而不是您。但是,你的上下文啓動失敗,堆內存不足,這很奇怪 - 通常是PermGen很快就會用完。也許這個'ThreadLocal'內存泄漏要嚴重得多?使用-XX:+ HeapDumpOnOutOfMemoryError來診斷問題。 – 2011-03-15 17:50:43

+0

實際上爲了管理上下文重新加載我在tomcat 中使用了reloadable =「false」。我知道任何Java代碼或屬性的改變都會迫使我重啓服務器,但它值得。 – Dejell 2011-03-17 09:15:26

6

添加以下到我的CDI @PreDestroy方法@ApplicationScoped豆,而當我關閉TomEE 1.6.0(tomcat7.0.39,截至今日),它會清除線程局部變量。

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package pf; 

import java.lang.ref.WeakReference; 
import java.lang.reflect.Array; 
import java.lang.reflect.Field; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

/** 
* 
* @author Administrator 
* 
* google-gson issue # 402: Memory Leak in web application; comment # 25 
* https://code.google.com/p/google-gson/issues/detail?id=402 
*/ 
public class ThreadLocalImmolater { 

    final Logger logger = LoggerFactory.getLogger(ThreadLocalImmolater.class); 

    Boolean debug; 

    public ThreadLocalImmolater() { 
     debug = true; 
    } 

    public Integer immolate() { 
     int count = 0; 
     try { 
      final Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); 
      threadLocalsField.setAccessible(true); 
      final Field inheritableThreadLocalsField = Thread.class.getDeclaredField("inheritableThreadLocals"); 
      inheritableThreadLocalsField.setAccessible(true); 
      for (final Thread thread : Thread.getAllStackTraces().keySet()) { 
        count += clear(threadLocalsField.get(thread)); 
        count += clear(inheritableThreadLocalsField.get(thread)); 
      } 
      logger.info("immolated " + count + " values in ThreadLocals"); 
     } catch (Exception e) { 
      throw new Error("ThreadLocalImmolater.immolate()", e); 
     } 
     return count; 
    } 

    private int clear(final Object threadLocalMap) throws Exception { 
     if (threadLocalMap == null) 
       return 0; 
     int count = 0; 
     final Field tableField = threadLocalMap.getClass().getDeclaredField("table"); 
     tableField.setAccessible(true); 
     final Object table = tableField.get(threadLocalMap); 
     for (int i = 0, length = Array.getLength(table); i < length; ++i) { 
      final Object entry = Array.get(table, i); 
      if (entry != null) { 
       final Object threadLocal = ((WeakReference)entry).get(); 
       if (threadLocal != null) { 
        log(i, threadLocal); 
        Array.set(table, i, null); 
        ++count; 
       } 
      } 
     } 
     return count; 
    } 

    private void log(int i, final Object threadLocal) { 
     if (!debug) { 
      return; 
     } 
     if (threadLocal.getClass() != null && 
      threadLocal.getClass().getEnclosingClass() != null && 
      threadLocal.getClass().getEnclosingClass().getName() != null) { 

      logger.info("threadLocalMap(" + i + "): " + 
         threadLocal.getClass().getEnclosingClass().getName()); 
     } 
     else if (threadLocal.getClass() != null && 
       threadLocal.getClass().getName() != null) { 
      logger.info("threadLocalMap(" + i + "): " + threadLocal.getClass().getName()); 
     } 
     else { 
      logger.info("threadLocalMap(" + i + "): cannot identify threadlocal class name"); 
     } 
    } 

} 
2

關鍵的「事務性資源」看起來像您正在與數據庫交談而沒有正確的事務。確保事務管理配置正確,並且不存在不在@Transactional註釋下運行的DAO調用路徑。當您在控制器級別配置事務管理但在計時器中調用DAO或正在使用@PostConstruct註釋時,很容易發生這種情況。我把它寫在這裏http://georgovassilis.blogspot.nl/2014/01/tomcat-spring-and-memory-leaks-when.html

編輯:它看起來像是(也?)一個彈簧數據jpa的bug已經修復與v1.4.3。我在設置「Transactional Resources」鍵的LockModeRepositoryPostProcessor的spring-data-jpa資源中查找它。在1.4.3中它也會再次清除密鑰。

0

當我們使用任何第三方解決方案而不使用處理程序進行清理活動時,會出現此問題。 對我來說這是EhCache發生的事情。我們在我們的項目中使用EhCache進行緩存。 而往往我們見慣了以下錯誤日誌中的

SEVERE: The web application [/products] appears to have started a thread named [products_default_cache_configuration] but has failed to stop it. This is very likely to create a memory leak. 
Aug 07, 2017 11:08:36 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/products] appears to have started a thread named [Statistics Thread-products_default_cache_configuration-1] but has failed to stop it. This is very likely to create a memory leak. 

我們經常注意到的tomcat失敗了,我們用來做後端的變化,並多次部署應用程序的反映了我們的發展變化過程中內存不足錯誤。

這是我們做

<listener> 
    <listener-class> 
    net.sf.ehcache.constructs.web.ShutdownListener 
    </listener-class> 
</listener> 

修復所以點我想說明的是檢查你使用的是第三方庫的文檔。他們應該提供一些機制在關機期間清理線程。你需要在你的應用程序中使用它。 沒有必要重新發明輪子,除非它們沒有提供。最糟糕的情況是提供你自己的實現。

爲參考的EHCache關機 http://www.ehcache.org/documentation/2.8/operations/shutdown.html