title: Java webapp memory leak when using ScheduledExecutorService
body: <p>My Tomcat 7 is reporting that there may be a memory leak in my webapp</p>
  
  <pre><code>SEVERE: The web application [/mywebapp] appears to have started a 
  thread named [pool-1-thread-1] but has failed to stop it. This is 
  very likely to create a  memory leak.
  </code></pre>
  
  <p>I have a long running task in my webapp that gets initialized when the webapp is started.</p>
  
  <pre><code>public class MyContextListener implements ServletContextListener{
  Scheduler scheduler = null;
  
  public MyContextListener(){
      scheduler = new Scheduler();
  }
  
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
      scheduler.stop();
  }
  
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
      scheduler.start();
  }
  
  }
  </code></pre>
  
  <p>.. and my Scheduler.java</p>
  
  <pre><code>public class Scheduler {
  private final ScheduledExecutorService fScheduler;
  
  public Scheduler() {
      fScheduler = Executors.newScheduledThreadPool(1);
  }
  
  
  public void start(){
      fScheduler.scheduleWithFixedDelay(new Runnable() {
  
          @Override
          public void run() {
              //Perform some task
          }
      }, 1, 240, TimeUnit.MINUTES);
  }
  
  public void stop(){
      fScheduler.shutdownNow();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Even though I calling <code>scheduler.stop();</code> when shutting down the server, its still reporting there could be a memory leak. </p>
  
  <p>This app is deployed on jelastic.com and I find that once it is started, it runs well for around two days and then the tasks don't seem to  be running. There is no exceptions or errors in the logs too.</p>
  
  <p>Am I doing anything wrong here ? Is there really a potential memory leak ?</p>