Tomcat memory leak

The following web applications were stopped (reloaded, undeployed),
 but their classes from previous runs are still loaded in memory, thus causing a memory leak (use a profiler to confirm): 

使用Maven 在Tomcat 上 重新部署項目時內存泄漏,原因之前的classes沒有卸載還存在 jvm 內存中,
不過在有博主說在卸載項目之前先 stop一下項目,再 undeploy 就不會出現 memory leak

Stack Overflow:

If you want to make sure not to cause leaks you have to do the following:

  1. Make sure your web application does not use any java classes that are in the web container shared libraries. If you have any shared libraries, make sure there is no strong references to the objects in those libraries
  2. Avoid using static variables, especially on java objects like HashTable, Sets, etc. If you need to, make sure that you call remove to release the objects with the maps, lists…
    Here is also a good article on ThreadLocal and MemoryLeaks - http://blog.arendsen.net/index.php/2005/02/22/threadlocals-and-memory-leaks-revisited/

1是避免使用容器中的庫,如果非要使用,避免使用強引用指向那些庫
2是避免使用靜態變量,特別是把map,set等容器作爲靜態變量

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章