`

Is this very likely to create a memory leak in Tomcat?

 
阅读更多
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-269] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-270] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-271] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-272] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-273] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-274] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-275] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-276] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-277] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-278] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-279] but has failed to stop it. This is very likely to create a memory leak.
May 26, 2015 4:12:52 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/scheduler] appears to have started a thread named [Timer-280] but has failed to stop it. This is very likely to create a memory leak.

 

这会导致Tomcat 服务器停止后Java进程尚未停止, 继续占用内存, 必须通过 类似 kill -9 之类的命令去杀死java进程.

 

这是由于在服务器停止时有些线程尚未销毁所引起的, 如ThreadLocal; Scheduler 启动的线程; JDBC driver等.

 

在stackoverflow 上有几篇 专门讨论这问题的帖子并提供了一些解决办法.

http://stackoverflow.com/questions/4899205/tomcat-6-memory-leaks-log-entries

http://stackoverflow.com/questions/11872316/tomcat-guice-jdbc-memory-leak

http://stackoverflow.com/questions/5292349/is-this-very-likely-to-create-a-memory-leak-in-tomcat

 

在吸取他们的解决办法的基础上, 作进一步的总结, 认为在web.xml中添加一个ServletContextListener在Context destroy的时候做一些事情是不错的, 具体代码如下:

 

public class ContextDestroyListener implements ServletContextListener {

    private static Logger logger = LoggerFactory.getLogger(ContextDestroyListener.class);

    public static final List<String> MANUAL_DESTROY_THREAD_IDENTIFIERS = Arrays.asList("QuartzScheduler", "scheduler_Worker");

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //Ignore
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        destroyJDBCDrivers();
        destroySpecifyThreads();
    }

    private void destroySpecifyThreads() {
        final Set<Thread> threads = Thread.getAllStackTraces().keySet();
        for (Thread thread : threads) {
            if (needManualDestroy(thread)) {
                synchronized (this) {
                    try {
                        thread.stop();
                        logger.debug(String.format("Destroy  %s successful", thread));
                    } catch (Exception e) {
                        logger.warn(String.format("Destroy %s error", thread), e);
                    }
                }
            }
        }
    }

    private boolean needManualDestroy(Thread thread) {
        final String threadName = thread.getName();
        for (String manualDestroyThreadIdentifier : MANUAL_DESTROY_THREAD_IDENTIFIERS) {
            if (threadName.contains(manualDestroyThreadIdentifier)) {
                return true;
            }
        }
        return false;
    }

    private void destroyJDBCDrivers() {
        final Enumeration<Driver> drivers = DriverManager.getDrivers();
        Driver driver;
        while (drivers.hasMoreElements()) {
            driver = drivers.nextElement();
            try {
                DriverManager.deregisterDriver(driver);
                logger.debug(String.format("Deregister JDBC driver %s successful", driver));
            } catch (SQLException e) {
                logger.warn(String.format("Deregister JDBC driver %s error", driver), e);
            }
        }
    }
}

 

来源于:http://blog.csdn.net/monkeyking1987/article/details/9182201

分享到:
评论

相关推荐

    微软内部资料-SQL性能优化2

    Although a system having less than 2 GB of physical memory can be booted using the /3G switch, in most cases this is ill-advised. If you restart with the 3 GB switch, also known as 4-Gig Tuning, the ...

    Direct Memory Access in Linux.doc

    This is likely due to the complex interactions between the kernel’s memory management and the physical memory layout. 3. **Kernel Modules and DMA Functions**: - **ioremap()**: This function maps ...

    How to be a Programmer

    I confine myself to problems that a programmer is very likely to have to face in her work. Many of these problems and their solutions are so general to the human condition that I will probably seem ...

    BURNINTEST--硬件检测工具

    - Enhanced the mechanism to report memory hardware errors in the Memory torture test. Release 5.3 build 1033 WIN32 release 1 October 2008 - Changes to correct a BurnInTest crash problem on some ...

    Android代码-dante

    This is the very first release of Dante, the api will (most likely) change for the better with successive iterations. Installation Simply add the following to your build.gradle: repositories { ...

    tomcat-5.5.23

    Change it to a port that isn't in use, and is greater than 1024, as ports less than or equal to 1024 require superuser access to bind under UNIX. &lt;br&gt; Restart Tomcat and you're in business...

    计算机组成与结构体系英文课件:Chapter 8 – The Memory System - 2

    Memory Hierarchy is a design strategy implemented to bridge the gap between the Central Processing Unit (CPU) and the various types of memory, which have differing speed, capacity, and cost ...

    Applied Text Analysis with Python

    is very likely to be an institution that produces fiscal and monetary tools in an economics, financial, or political domain, whereas in an aviation or vehicular domain it is more likely to be a form ...

    a project model for the FreeBSD Project.7z

    This, combined with the vast amount of dependencies in the kernel and that it is not easy to see all the consequences of a kernel change, demands developers with a relative full understanding of the ...

    applied wpf 4 in 2010

    In this book, I will show you how to create and implement WPF, using best practices to create a realworld application. At the same time, I’ll also show you how to structure and architect a WPF ...

    The Princeton Companion To Mathematics

    In addition to these, mathematics uses a notion which is not a constituent of the propositions which it considers, namely the notion of truth. The Princeton Companion to Mathematics could be said to ...

    算法导论答案 麻省理工

    It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send a message ...

    Preproduction.Blueprint.How.to.Plan.Game.Environments.and.Level.Designs.2nd.Ed

    This could be for a playable level or a game environment exploration to show off in a portfolio. Planning process is called pre-production and what you end up with is a "Preproduction Blueprint". It...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    skyboxmove.unitypackage

    In this article, I will introduce how to use ShaderForge to create a flowing clouds animation. The completed animation will become like this. Let’s start creating this animation and set it so ...

    A Course in Group Theory

    This book fills the gap, providing a clear and comprehensive introduction to the theory of groups and covering all topics likely to be encountered in undergraduate courses. Introductory chapters ...

    《算法导论》习题解答.rar

    It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send a message ...

    10231A.Designing.A.Microsoft.Sharepoint.2010.Infrastructure.Vol1

    This course is intended for IT Professionals who use Microsoft SharePoint 2010 in a team-based, medium-sized to large environment. While they may have implemented a SharePoint deployment, they have ...

    Python_Real-World+Data+Science_A+Course+in+Four+Modules-2016.pdf

    I love to make references to the real world when I teach coding; I believe they help people retain the ...we need to know is that every object in Python has an ID (or identity), a type, and a value.

Global site tag (gtag.js) - Google Analytics