Memory leaks 内存泄漏tomcat 7
当我运行Tomcat 7时,会收到以下错误消息:Memory leaks 内存泄漏tomcat 7,memory-leaks,tomcat7,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.SchedulerFactor
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.
您应该在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>
yourpackage.AppListener
或者,您可以在带有@PreDestroy注释的方法中关闭调度程序。您应该在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>
yourpackage.AppListener
或者,您可以关闭带有@PreDestroy注释的方法中的调度程序,SchedulerFactoryBean至少从Spring 3.0开始就有一个标志告诉它等待作业完成。设置标志并确保在关机时调用销毁方法:
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown" value="true" />
....
....
请参阅:至少从Spring 3.0开始,SchedulerFactoryBean就有一个标志告诉它等待作业完成。设置标志并确保在关机时调用销毁方法:
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown" value="true" />
....
....
见: