service - Android 10 中的 LocalService 和 LocalBinder 泄漏内存
问题描述
我有一个LocalService
完全按照这里建议的实现,以便通过活页夹提供对服务方法的访问。
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;
}
}
从 Android 10 开始,此实现似乎会泄漏内存。当服务未绑定(和销毁)时,LocalService
不会对LocalBinder
对象进行垃圾收集。下一个绑定创建一个新的服务对象。根据内存分析器,该对象在 Cleaner 中LocalBinder
是引用的。知道如何解决吗?
解决方案
推荐阅读
- c++ - 是否可以从某个时间点从文件中读取日期?例如。我想阅读文件的第 7 个字母。C++
- neo4j - Neo4j 按查询性能排序不佳
- c++ - 找到数 n 小于 k 的除数的有效方法
- vnc - 如何在没有以太网端口的情况下将 jetson nano 与 vnc 查看器连接
- c++ - 如何修复此错误:未分配被释放的指针
- python - 用于创建掩码的数组的良好初始化方案
- r - R - 协变量多于观测值的贝叶斯树
- android - Android资源编译失败我构建时没有错误但是在运行程序时出现错误
- android - 当我更改应用名称时会发生什么?
- javascript - 为什么 Сhrome 显示 JavaScript setAttribute() 错误