Threads are going to be renewed over time to try and avoid a probable memory leak.
記錄一次記憶體洩漏問題的解決
問題現象:啟動tomcat後,過幾個小時,線上的工程就會停止執行了,日誌裡面就會報下面這樣的錯誤
13-Aug-2019 12:51:27.248 SEVERE [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4a9cfa60]) and a value of type [com.daichao.loans.api.common.AuthenticationType] (value [TOKEN]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
這個是由於類載入器洩漏導致的的,從上面的問題可以看出來當應用結束的時候,想釋放AuthenticationType,但是卻釋放不了,為什麼釋放不了呢?
先看程式碼:
先在LoginUtils裡面定義了一個ThreadLocal
public static final ThreadLocal<AuthenticationType> authenticationType
= new ThreadLocal<AuthenticationType>();
再到SystemController類裡設定值
LoginUtils.authenticationType.set(authenticationType);
最後再到MyRealm裡取值
AuthenticationType authTypeClass = LoginUtils.authenticationType.get();
tomcat有一個執行緒池,當有外部請求來的時候,會從執行緒池裡面取出一個執行緒來處理請求,ThradLocal引用了AuthenticationType,執行緒對ThradLocal設值,相當於將AuthenticationType繫結到了這個執行緒,當應用結束了,想去回收AuthenticationType的classloader,即使已經沒有其他地方對AuthenticationType的引用了,但執行緒還沒有結束,這個執行緒還對AuthenticationType有引用,tomcat回收資源的時候,發現AuthenticationType回收不了,就會出現記憶體洩漏的警告。
最後的解決方法是:當應用結束的時候手動釋放ThreadLocal裡面的AuthenticationType
try{
AuthenticationType authTypeClass = LoginUtils.authenticationType.get();
}finally{
LoginUtils.authenticationType.remove();
}
參考文章:https://blog.csdn.net/u010619243/article/details/62899033
相關文章
- mormot.core.threads--TSynBackgroundTimerORMthread
- where are you going ? 反序為:going you are whereGo
- Monitor RDBMS Session UGA and PGA Current And Maximum Usage Over TimeSession
- Lock wait timeout exceeded; try restarting transactionAIREST
- CLR Threads and Windows ThreadsthreadWindows
- mysql, Lock wait timeout exceeded; try restarting 解決MySqlAIREST
- 'Lock wait timeout exceeded; try restarting transaction'問題AIREST
- Lock wait timeout exceeded; try restarting transaction引數控制AIREST
- D - Avoid K Palindrome
- Oracle_TimesTen_and_In-Memory_Database_Cache_11g_Ed_1OracleDatabase
- 轉:How is Internat Bank going?Go
- MySql報錯:Lock wait timeout exceeded: try restadina transactionMySqlAIREST
- Probable reasons when Credit check is not working
- row_number() over,rank() over,dense_rank() over的區別
- sum()over()和count()over()分析函式函式
- LCA Online Query with O(N) Memory and O(1) Time Complexity
- over (partition by)
- Import OverImport
- 執行緒 threads執行緒thread
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transactionErrorAIREST
- 【故障處理】ORA-28547: connection to server failed, probableServerAI
- Common threads: Awk by examplethread
- mormot.core.threads--TSynQueueORMthread
- mormot.core.threads--TSynParallelProcessORMthreadParallel
- Laravel try catchLaravel
- Jumping with Try
- js try catchJS
- Java之建立物件>7.Avoid finalizersJava物件
- mysql自動排序函式dense_rank() over()、rank() over()、row_num() over()用法和區別MySql排序函式
- Getting going quickly with Python, MongoDB and Spatial dataUIPythonMongoDB
- Simplifying Graphics With Java and Threads (轉)Javathread
- MySQL 5.5 -- innodb_purge_threadsMySqlthread
- mormot.core.threads--TBlockingProcessORMthreadBloC
- mormot.core.threads--TSynThreadORMthread
- mormot.core.threads--TSynThreadPoolORMthread
- mormot.core.threads.pas unitORMthread
- 分析函式 over函式
- iOS的@try、@catch()iOS