The web application [] appears to have started ...

本文探讨了Tomcat中因线程引用导致的内存泄漏问题。详细解释了当Web应用程序停止时,其类加载器仍被子线程引用的情况,并提供了两种解决方法:一是设置线程上下文类加载器为null;二是实现线程内部的停止机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 The web application [] appears to have started ...
  • 发布时间: 2012/07/25 11:51 
  • 阅读: 468 
  • 收藏: 0 
  • 点赞: 0 
  • 评论: 0

    有看到过这个标题的人吗?我的Tomcat出什么问题了?

    有一个servlet,它在init方法里面实例化了一个线程类。这个类里面启动了若干个daemon线程。

    有一个listener,它在contextDestroyed方法里面关闭这个线程类。

    在catalina.out里面出现了这样的日志。

    在下面的这个link里面,apache官方给了一些说明:

    http://wiki.apache.org/tomcat/MemoryLeakProtection

    其中就包含了这种问题。具体的原因说的很清楚:

    

 Here, when the app is stopped, the webapp classloader is still referenced by the spawned thread both through its context classloader and its current call stack (the anonymous Thread subclass is loaded by the webapp classloader). When stopping an application, tomcat checks the context classloader of every Thread, and if it is the same as the app being stopped, it logs the following message :  

    当前app的classloader的引用指向到里我的线程类。

    修改方法参照官方说明。

    

leakingThread.setContextClassLoader(null)

    还有另外一种修改方法。

    在我们的线程类里面加入stop方法: 

   

class Example implements Runnable {
        private volatile boolean isStop;
        private Thread runThread;

        @Override
        public void run() {
            runThread = Thread.currentThread();
            isStop = false;
            while(!isStop){
                try {
                    process();
                } catch (XMPPException e) {
                    log.error("Example logic exception.", e);
                }
            }
        }

        public void stopRun() {
        	isStop = true;
        	if(runThread != null) {
        		runThread.interrupt();
        	}
        }

    然后在contextDestroyed方法里面调用stopRun方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值