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
相關文章
- Love Memory-Sweet Time
- LCA Online Query with O(N) Memory and O(1) Time Complexity
- D - Avoid K Palindrome
- Avoid autocompleting of the dates in plot
- TRy
- 【故障處理】ORA-28547: connection to server failed, probableServerAI
- mormot.core.threads--TSynBackgroundTimerORMthread
- mormot.core.threads--TSynQueueORMthread
- mormot.core.threads--TBlockingProcessORMthreadBloC
- mormot.core.threads--TSynBackgroundThreadMethodORMthread
- mormot.core.threads--TSynParallelProcessORMthreadParallel
- mormot.core.threads--TSynThreadORMthread
- mormot.core.threads--TSynThreadPoolORMthread
- Threads in Node 10.5.0: a practical introthread
- Jumping with Try
- try ,catch
- VisualVM第二章-ThreadsLVMthread
- mormot.core.threads.pas unitORMthread
- memory
- mysql自動排序函式dense_rank() over()、rank() over()、row_num() over()用法和區別MySql排序函式
- Jenkins Publish Over SSHJenkins
- Laravel try catchLaravel
- Is it going on-------------Docker容器的安裝以及基本的操作指令GoDocker
- BMZCTF:memory
- time time_t tm用法
- {"error":400,"message":"over quota"}Error
- Review: Red Star over ChinaView
- [Memory Leak] 1. console.log cause memory leak
- VM warning: INFO: OS::commit_memory Cannot allocate memoryMIT
- HITB2021|綠盟科技議題:Going Deeper into Schneider Modicon PAC SecurityGoIDE
- memory-cnblog
- Memory Management in RustRust
- OOM(Out Of Memory)OOM
- MongoDB 啟動時關於 NUMA 警告 的分析----(To avoid performance problems)MongoDBORM
- MQTT over QUIC 多流支援MQQTUI
- SQL-ROWNUMBER-OVER彙總SQL
- Flink SQL之Over 聚合操作SQL
- ROWNUMBER() OVER( PARTITION BY COL1