简单的webapp中内存泄漏的原因是什么?

时间:2014-03-30 09:47:20

标签: java tomcat memory-leaks

非常简单的webapp,但tomcat在取消部署后发现内存泄漏。

  

以下Web应用程序已停止(重新加载,取消部署),   但是他们以前运行的类仍然在内存中加载   导致内存泄漏(使用分析器确认):/ WEBSearchPrime_3

是什么原因?

package mainThread;

public class GeneralThread implements Runnable {

    private boolean stop = false;

    @Override
    public void run() {

        while (!Thread.interrupted()) {

            try {
                Thread.sleep( 1000 );
            } catch (InterruptedException e) { }

            if(isStop()) {
                System.out.println("Exit");
                return;
            }
            System.out.println("DO TASK");
        }
    }

    public boolean isStop() {
        return stop;
    }

    public synchronized void setStop() {
        this.stop = true;
    }

}

package mainThread;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AppServletContextListener implements ServletContextListener {

    Thread thread;
    GeneralThread generalThread;

    @Override
    public void contextInitialized(ServletContextEvent arg0) {  
        generalThread = new GeneralThread();
        thread = new Thread(generalThread);
        thread.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

        generalThread.setStop();
        thread.interrupt();

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Context destroyed!");
        if(thread.isAlive()) System.out.println("thread is still alive...");
    }
}

0 个答案:

没有答案