title: Minimal android foreground service killed on high-end phone
body: <p>I'm trying to create an app that lets users log routes (locations/GPS). To ensure locations are logged even when the screen is off, I have created a <code>foreground service</code> for the location logging. I store the locations in a <code>Room Database</code> which is injected into my service using <code>Dagger2</code>.</p>
  
  <p>However, this service is killed by Android which is, of course, not good. I could subscribe to low memory warnings but that doesn't solve the underlying problem of my service getting killed after ~30 minutes on a modern high-end phone running Android 8.0</p>
  
  <p>I have created a minimal project with only a "Hello world" activity and the service: <a href="https://github.com/RandomStuffAndCode/AndroidForegroundService" rel="noreferrer">https://github.com/RandomStuffAndCode/AndroidForegroundService</a></p>
  
  <p>The service is started in my <code>Application</code> class, and route logging is started through a <code>Binder</code>:</p>
  
  <pre><code>// Application
  @Override
  public void onCreate() {
      super.onCreate();
      mComponent = DaggerAppComponent.builder()
              .appModule(new AppModule(this))
              .build();
  
      Intent startBackgroundIntent = new Intent();
      startBackgroundIntent.setClass(this, LocationService.class);
      startService(startBackgroundIntent);
  }
  
  // Binding activity
  bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
  // mConnection starts the route logging through `Binder` once connected. The binder calls startForeground()
  </code></pre>
  
  <p>I probably don't need the <code>BIND_AUTO_CREATE</code> flag, I've been testing different flags in an attempt to not get my service killed - no luck so far.</p>
  
  <p>Using the profiler it does not seem like I have any memory leaks, memory usage is stable at ~35mb:</p>
  
  <p><a href="https://i.stack.imgur.com/R5QGQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/R5QGQ.png" alt="profiler"></a></p>
  
  <p>Using <code>adb shell dumpsys activity processes &gt; tmp.txt</code> i can confirm that <code>foregroundServices=true</code> and my service is listed 8th in the LRU list:</p>
  
  <p><code>Proc # 3: prcp  F/S/FGS  trm: 0 31592:com.example.foregroundserviceexample/u0a93 (fg-service)</code></p>
  
  <p>It seems like it is not possible to create a foreground service that you can trust to not get killed. So what can we do? Well...</p>
  
  <ol>
  <li>Put the service in a separate process, in an attempt to let Android kill the UI/Activities while leaving the service alone. Would probably help, but doesn't seem like a guarantee</li>
  <li>Persist <em>everything</em> in the service in e.g. a Room database. Every variable, every custom class, every time any of the changes and then start the service with <code>START_STICKY</code>. This seems kind of wasteful and doesn't lead to very beautiful code, but it would probably work... somewhat. Depending on how long it takes for Android to re-create the service after killing it, a large portion of locations may be lost.</li>
  </ol>
  
  <p>Is this really the current state of doing stuff in the background on Android? Isn't there a better way?</p>
  
  <p>EDIT: Whitelisting the app for battery optimization (disabling it) does not stop my service from being killed</p>
  
  <p>EDIT: Using <code>Context.startForegroundService()</code> to start the service does not improve the situation</p>
  
  <p>EDIT: So this indeed only occurs on <em>some</em> devices, but it occurs consistently on them. I guess you have to make a choice of either not supporting a huge number of users or write really ugly code. Awesome.</p>