I have created a bound service exactly as described in the Android documentation here: https://developer.android.com/guide/components/bound-services.html#Binder
My code is exactly the same as the code in the examples, down to the letter. I have the same LocalBinder inner class extending Binder and returning the enclosing Service. My activity binds to the service, gets the reference to the service from the binder in a ServiceConnection callback, and uses the reference to call methods in the service. It binds in onStart and unbinds in onStop, just as shown in the example code.
When my Activity exits, it sends a local broadcast. When the service receives that broadcast, it calls stopForeground() (it is a foreground service), and then calls stopSelf(). Its onDestroy() method is then called. However, there is a problem. The service's LocalBinder instance is retained in memory, because it is referred to by Android's FinalizerReference. Lots of other imported and system classes also stay in memory.
Does anyone know why the LocalBinder reference is kept in the FinalizerReference queue? And is this what is causing the other classes to remain in memory? I have tried making LocalBinder a separate class rather than an inner class, but this hasn't helped. Thanks.
Edit: the problem still occurs when the service is not a foreground service.