内存泄漏tomcat 7

时间:2015-08-04 10:57:45

标签: memory-leaks tomcat7

当我运行Tomcat 7时,收到此错误消息:

Aug 04, 2015 12:53:47 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
SEVERE: The web application [/sample] appears to have started a thread named [org.springframework.scheduling.quartz.SchedulerFactoryBean#5_Worker-9] but has failed to stop it. This is very likely to create a memory leak.

2 个答案:

答案 0 :(得分:0)

你应该优雅地关闭ServletContextListener中的quartz,如下所示:

public class AppListener implements ServletContextListener
{

@Override
public void contextDestroyed(ServletContextEvent arg0)
{
    try
    {
        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        Scheduler scheduler = (Scheduler) context.getBean("quartzSchedulerFactory");
        scheduler.shutdown(true);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

@Override
public void contextInitialized(ServletContextEvent arg0)
{
} 

您必须在web.xml中添加这些ServletContextListener。您应该将侦听器放在web.xml的第一个位置,因为tomcat在关闭时以相反的顺序运行侦听器。

<listener>
    <listener-class>yourpackage.AppListener</listener-class>
</listener>

或者,您可以在使用@PreDestroy注释的方法中关闭调度程序

答案 1 :(得分:0)

SchedulerFactoryBean 至少从 Spring 3.0 开始就有一个标志告诉它等待作业完成。设置标志并确保将在关闭时调用 destroy 方法:

<bean id="scheduler"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
        destroy-method="destroy">
    <property name="waitForJobsToCompleteOnShutdown" value="true" />
    ....

见:https://docs.spring.io/spring-framework/docs/3.0.0.RELEASE/javadoc-api/org/springframework/scheduling/quartz/SchedulerFactoryBean.html#setWaitForJobsToCompleteOnShutdown(boolean)