title: Android service stops
body: <p>I've come to the point where I'm a few steps away from going bald...</p>
  
  <p>I'm trying to run a service that will check if the servers at my work are running. It's supposed to do this every 5 minutes.</p>
  
  <p>I've been through TimerTask, ScheduledExecutorService and finally Handler.
  They all work "fine" for a few hours, except for some inaccuracy, 1-5 mins off, and then suddenly the "timer" just stops firing.</p>
  
  <p>Now, I've read that the Scheduler will stop if it encounters an uncaught Exception, and I'm sure that's the case as well with the TimerTask - But checking the log files, there aren't any exceptions at all...</p>
  
  <p>When I got home from work today I decided to do an experiment with Handler.</p>
  
  <p>I created 2 handlers, one would run the code for checking the servers and increment an int one at a time. The other would just Log the value of said int, so as to not stop should it encountered an exception (I can't see how it would though).</p>
  
  <p>It runs great, with the usual 1-5 mins inaccuracy, for a few hours and then stops, below is the last lines from the log:</p>
  
  <pre><code>02-07 20:03:25.892 D/dalvikvm(  992): GC_EXPLICIT freed 192K, 53% free 4295K/9031K, external 3755K/4825K, paused 114ms
  02-07 20:03:35.572 D/dalvikvm( 5472): GC_EXPLICIT freed &lt;1K, 54% free 2895K/6279K, external 2002K/2137K, paused 61ms
  02-07 20:04:42.212 V/ServerChecker(12568): Timer triggered
  02-07 20:04:42.212 V/ServerChecker(12568): Checking: linux15
  02-07 20:04:44.152 V/ServerChecker(12568): Checked: linux15
  02-07 20:04:44.152 V/ServerChecker(12568): Checking: linux1
  02-07 20:04:44.462 V/ServerChecker(12568): Checked: linux1
  02-07 20:04:44.462 V/ServerChecker(12568): Checking: linux12
  02-07 20:04:44.762 V/ServerChecker(12568): Checked: linux12
  02-07 20:04:44.762 V/ServerChecker(12568): Checking: linux9
  02-07 20:04:45.072 V/ServerChecker(12568): Checked: linux9
  02-07 20:04:45.072 V/ServerChecker(12568): Checking: linux14
  02-07 20:04:45.382 V/ServerChecker(12568): Checked: linux14
  02-07 20:04:45.382 V/ServerChecker(12568): Test timer triggered: 13
  02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
  02-07 20:05:01.002 E/InputDispatcher(  223): channel '406cefc8 com.n04dev.serverchecker/com.n04dev.serverchecker.ServerChecker (server)' ~ Channel is unrecoverably broken and will be disposed!
  02-07 20:05:08.932 D/dalvikvm(12842): GC_EXPLICIT freed 73K, 51% free 2641K/5379K, external 2002K/2137K, paused 37ms
  02-07 20:05:09.132 D/dalvikvm(  185): GC_EXPLICIT freed 11K, 53% free 2554K/5379K, external 2002K/2137K, paused 96ms
  02-07 20:05:12.022 D/dalvikvm(  185): GC_EXPLICIT freed &lt;1K, 53% free 2554K/5379K, external 2002K/2137K, paused 164ms
  02-07 20:05:12.062 D/dalvikvm(  185): GC_EXPLICIT freed &lt;1K, 53% free 2554K/5379K, external 2002K/2137K, paused 36ms
  02-07 20:05:18.612 D/dalvikvm(12852): GC_EXPLICIT freed 59K, 52% free 2596K/5379K, external 2002K/2137K, paused 72ms
  </code></pre>
  
  <p>I've looked up the last two messages concerning my app and found <a href="https://stackoverflow.com/questions/4484444/what-does-this-error-mean-android">this thread</a> - But I can't see how that would apply in my case.</p>
  
  <p>I really hope you guys have some idea about what I'm doing wrong. I've been at it for weeks.. Trying different timers, having a timer try to catch the "main" timer stopping and then restarting it, but with no luck, and as my last experiment shows, it doesn't seem to be a problem with the timer.. I think..</p>