I have a LocalService
implementation exactly as suggested here in order to provide access to service methods through the binder.
https://developer.android.com/guide/components/bound-services#Binder
public class LocalService extends Service {
// Binder given to clients
private final IBinder binder = new LocalBinder();
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
LocalService getService() {
// Return this instance of LocalService so clients can call public methods
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
Starting Android 10 this implementation seems to leak memory. When the service is unbound (and destroyed) the LocalService
and LocalBinder
objects are not garbage collected. The next bind creates a new service object. According to the memory profiler the LocalBinder
object is referent in Cleaner. Any idea how to fix?