title: Quartz Scheduler Memory Leak in Tomcat
body: <p>I use quartz version <code>2.2.2</code> and Spring Boot version <code>1.3.1</code>. It behaves as it should and all works fine. But the problem arises when i try to shut down this application. Log shows there is a memory leak...</p>
  
  <p>My Quartz Configuration;</p>
  
  <pre><code>org.quartz.scheduler.instanceName = my-app-jobs
  org.quartz.scheduler.instanceId = AUTO
  org.quartz.threadPool.threadCount=2
  org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
  org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
  org.quartz.scheduler.skipUpdateCheck = true
  </code></pre>
  
  <p>I have 2 Classes which implement job like below;</p>
  
  <pre><code>/**
   *
   * To Run Every 15 Minutes
   *
   */
  public class MemoryDataUpdateJob implements Job {
  
      @Autowired
      private Dao dao;
  
      @Override
      public void execute(JobExecutionContext context) throws JobExecutionException {
          Logger.info(getClass().getName(), "Scheduled Memory Data Update Job Started...");
  
          //Read Device Settings
          dao.readDeviceSettings();
  
          Logger.info(getClass().getName(), "Scheduled Memory Data Update Job Finished.");
      }
  
  }
  </code></pre>
  
  <p>Other Class is also similar doing different stuff.</p>
  
  <p>Scheduler Factory Bean as follows;</p>
  
  <pre><code>@Bean
  public SchedulerFactoryBean schedulerFactoryBean(
          JobFactory jobFactory,
          @Qualifier("MemoryDataUpdateJobTrigger") Trigger memoryDataUpdateJobTrigger,
          @Qualifier("MsgCountJobTrigger") Trigger msgCountJobTrigger) throws IOException {
      SchedulerFactoryBean factory = new SchedulerFactoryBean();
      factory.setJobFactory(jobFactory);
      factory.setQuartzProperties(quartzProperties());
      factory.setTriggers(memoryDataUpdateJobTrigger, msgCountJobTrigger);
      return factory;
  }
  </code></pre>
  
  <p>I was getting tomcat log like below;</p>
  
  <pre><code>19-Jan-2016 11:18:39.722 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp-1.0.0] appears to have started a thread named [schedulerFactoryBean_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
   java.lang.Object.wait(Native Method)
  
  19-Jan-2016 11:18:39.722 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MyApp-1.0.0] appears to have started a thread named [schedulerFactoryBean_Worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
   java.lang.Object.wait(Native Method)
  </code></pre>
  
  <p>Went through quartz documentation and added the following in properties;</p>
  
  <pre><code>org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
  org.quartz.plugin.shutdownhook.cleanShutdown = true
  </code></pre>
  
  <p>The First thread message disappeared but second message about <code>worker-2</code> memory leak is still there.</p>
  
  <p>But in my Application Logger i see something like this;</p>
  
  <pre><code>[2016-01-19 11:18:39:681] [DEBUG] [schedulerFactoryBean_Worker-1] [org.quartz.simpl.SimpleThreadPool:612] - WorkerThread is shut down.
  
  [2016-01-19 11:18:40:047] [DEBUG] [schedulerFactoryBean_Worker-2] [org.quartz.simpl.SimpleThreadPool:612] - WorkerThread is shut down.
  
  [2016-01-19 11:18:40:049] [INFO ] [Quartz Shutdown-Hook schedulerFactoryBean] [org.quartz.plugins.management.ShutdownHookPlugin:126] - Shutting down Quartz...
  </code></pre>
  
  <p>What is the problem here? Why there is a memory leak even if the application logger shows threads are shutdown?</p>