8

以下是我在 CentOS 机器上的 Catalina.out 文件中的唯一条目。我正在使用 spring 3 和我的应用程序运行 Tomcat 6。有一大堆,所以我只选择了一些不断重复的。这不会一直发生,但至少每周发生一次。

问题是我能做些什么来防止波纹管的发生?

Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc

SEVERE: The web application [] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

SEVERE: The web application [] appears to have started a thread named [com.iteezy.shared.domain.DirEntry.data] but has failed to stop it. This is very likely to create a memory leak.


Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [] appears to have started a thread named

[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak.


Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads  

SEVERE: The web application [] appears to have started a thread named [File Reaper] but has failed to stop it. This is very likely to create a memory leak.

Feb 3, 2011 2:37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

SEVERE: The web application [] appears to have started a thread named [pool-1-thread-22] but has failed to stop it. This is very likely to create a memory leak.  

37:48 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
b application [] appears to have started a thread named 

[org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2] but has failed to stop it. This is very likely to create a memory leak.

37:48 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap

b application [] created a ThreadLocal with key of type [net.sf.json.AbstractJSON$1] (value [net.sf.json.AbstractJSON$1@40bbb3d6]) and a value of type [java.util.HashSet] (value [[]]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
4

3 回答 3

12

当您定义一个线程应该在设置时轮询并退出的外部标志时 -它必须是volatile. 否则线程可能永远不会看到其他线程所做的更改。

然而,在标准 API 中已经有一个类似的特性——它被称为interrupt()方法和Thread.currentThread().isInterrupted(). 无需复制已经存在的逻辑。请参阅:停止特定的 java 线程

话虽如此,调用interrupt()每个线程也是一个坏主意,因为不能保证所有线程都会响应它。检查你的异常我注意到以下线程没有被正确清理:

  • com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0- 关闭 C3P0 数据源。由于您使用的是 Spring,因此只需添加destroy-method="close". 我们已经完成了这个线程。

  • File Reaper- 据我所知,这个线程是由FileCleaningTracker创建的。您需要FileCleaningTracker.exitWhenFinished()在关闭应用程序时显式调用(或者当不再需要该类时,我从未使用过它)或让 Spring 执行此操作(见上文)。有可能一些 3rd 方库使用它并且没有正确关闭 - 这意味着它有一个错误。

  • pool-1-thread-22- 这是Executors实用程序内部创建的线程之一ExecutorService。确保shutdown()在关闭期间调用应用程序中的每个这样的池。

  • org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2- Quartz 工作线程(实际运行作业的线程)。SchedulerFactoryBean自动为您关闭调度程序,我认为 Tomcat 在这里弄错了,我也经常看到这个错误。不过看起来像设置SchedulerFactoryBean.waitForJobsToCompleteOnShutdowntrue解决这个问题。

  • com.iteezy.shared.domain.DirEntry.data- 我不确定这个。要么是你自己的线程需要在关闭时中断,要么是 H2 数据库线程(?)需要检查它的堆栈以猜测它来自哪里。

底线是:不要只杀死所有移动的东西(实际上,Tomcat 在发出此警告后会为您执行此操作),而是确定线程来自哪里并使用框架/库特定close()的方法来允许进一步清理。要温柔。

于 2011-10-27T07:57:26.247 回答
1

设置一个 Servlet 以在其destroy()方法中管理它。线程可以检查标志以查看它们是否必须继续。

在您的 servlet 中,在 destroy 方法中执行以下操作。您显然需要能够访问 aCollection<MyThread>但如何获得它实际上取决于您的系统是如何设置的。

destroy() {
    for (MyThread thread : myThreads) {
        thread.stopProcessing();
    }
}

你的MyThread班级会有这样的事情:

public class MyThread {
    private boolean finished = false;

    @Override
    public void run() {
        while (!finished) {
            //do something
        }
    }

    public void stopProcessing() {
        finished = true;
    }
}
于 2011-02-04T14:56:30.190 回答
-1

正确关闭您的网络应用程序。不要让这些线程继续运行。

或者,不要继续重新部署 webapp。

于 2011-02-04T14:45:18.240 回答