title: Endless running foreground service keeps Google Play services for ads in memory
body: <p><strong>TL DR</strong></p>
  <p>I have an app that runs a foreground service forever to monitor the battery (temperature, level, etc), logging this data and send notifications if certain limits are exceeded. Since I added adverts, the running service is using a high amount of memory even after closing the app and finally gets killed, which makes my app useless. I searched for hours and couldn't find anyone reporting something similar, so I guess I'm doing something wrong.</p>
  <hr />
  <p>I use MVVM pattern with a single activity and multiple fragments in a bottom navigation view. In <code>MainActivity</code>, onCreate() I initialize the service:</p>
  <pre><code> val serviceIntent = Intent(this, MyService::class.java)
   if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
         startForegroundService(serviceIntent)
   } else {
         startService(serviceIntent)
   }
  </code></pre>
  <p>In <code>MyService.kt</code> I call</p>
  <pre><code>override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
          createNotificationChannels()
          startForegroundForService()
      } else {
          startForeground(1, Notification())
      }
      return START_REDELIVER_INTENT
  }
  
  
  private fun startForegroundForService() {
              val notificationBuilder = NotificationCompat.Builder(this, TEMP_NOTIFICATION_CHANNEL_ID)
          val notification = notificationBuilder.setOngoing(false)
                  .setPriority(NotificationManager.IMPORTANCE_LOW)
                  .setCategory(Notification.CATEGORY_SYSTEM)
                  .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                  .setOnlyAlertOnce(true)
                  .setContentIntent(resultPendingIntent)
                  .build()
          startForeground(TEMP_NOTIFICATION_ID, notification)
  }
  </code></pre>
  <p>This works fine and the service keeps running for days if the app is excluded from the battery optimization. The memory used is around 50MB as I can check in the developer options in &quot;Running services&quot;.</p>
  <p>The problems started when I added banner ads in some fragments. In <code>MainActivity</code> I call <code>MobileAds.initialize(this)</code> and in each fragment this code:</p>
  <pre><code>private lateinit var mAdViewBanner: AdView
  
      override fun onCreateView(
              inflater: LayoutInflater,
              container: ViewGroup?,
              savedInstanceState: Bundle?
      ): View? {
          .....
          mAdViewBanner = binding.adBanner
          ....
      }
  
      override fun onPause() {
          super.onPause()
          mAdViewBanner.pause()
      }
  
      override fun onDestroy() {
          mAdViewBanner.destroy()
          super.onDestroy()
      }
  
      override fun onResume() {
          super.onResume()
          mAdViewBanner.resume()
      }
  </code></pre>
  <p>I added log lines to check if these lines are called when navigating through the app, including onDestroy in the MainActivity when the app gets closed via back button. The home fragment gets destroyed after the MainActivity, otherwise, I haven't seen any strange behaviour. The banner ads are destroyed (or at least their instances are called to be destroyed).</p>
  <p>The service remains running with 4 processes of which two are Google Play services. The used memory amount of the app is around 400MB (instead of the 50MB expected). These Play services don't stop, even after 30minutes without opening the app. Some time later all services and processes get killed.</p>
  <p>I couldn't find a way to remove these Google Play services on closing the app. The app shouldn't use such a high amount of memory when running as a foreground service and being closed.</p>
  <p>I tried to run the service in a separate process. This solved the problem with the high RAM amount and the Play Services but stopped the app from working properly. The Room database is not written anymore as I call in the service write operations to the app repository.
  Is there any other way to do this? I haven't tried IPC, but I guess this will not solve the first problem because then I have to keep running a second process for the database which could keep the Play Services running.</p>