android - LeakCanary 为 AppOpsManager OnOpChangedListener 回调函数发现内存泄漏
问题描述
我在我的 Android 应用程序中发现了可用的内存泄漏,LeakCanary 总是说 AppOpsManager OnOpChangedListener 回调函数对 MainActivity 有泄漏。我在 google 和 SF 上搜索了很多,但没有找到任何解决方案。下面是我的代码,请帮助解决这个内存泄漏。
@Override
protected void onCreate(Bundle savedInstanceState) {
appOpsManager = (AppOpsManager) getApplicationContext().getSystemService(Context.APP_OPS_SERVICE);
appOpsManager.startWatchingMode(AppOpsManager.OPSTR_GET_USAGE_STATS, getApplicationContext().getPackageName(), usageOpListener);
}
private final AppOpsManager.OnOpChangedListener usageOpListener = new AppOpsManager.OnOpChangedListener() {
@Override
public void onOpChanged(String op, String packageName) {
if (packageName == null || getApplicationContext().getPackageName().equals(packageName)) {
if (AppOpsManager.OPSTR_GET_USAGE_STATS.equals(op)) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
getApplicationContext().startActivity(myIntent);
}
}
}
};
@Override
protected void onDestroy() {
appOpsManager.stopWatchingMode(usageOpListener);
}
解决方案
I believe (although you haven't shown it) that the problem is:
- You register a local object to your instance (
usageOpListener
) to another object that you obtained indirectly through the system locator service (appOpsManager
) during your class'onCreate
. - This reference to
YourActivity#OnOpChangedListener
is now a hard reference inside theAppOpsManager
. - You then expect the reference to go away when your activity is destroyed, but your activity is not always destroyed by the Framework. It can be stopped, but the hard reference to it, is still present in the AppOps Manager.
I'd move the start/stop watching code to onStart/onStop respectively.
推荐阅读
- sql - HANA SQL 子查询
- jmeter - 非 gui 模式在我的 Jmeter 测试中没有优势吗?
- pytorch-lightning - Pytorch Lightning 自动记录 - AttributeError:“NoneType”对象没有属性“_results”
- php - 在 centos 7 上安装 php-gmp 失败
- perl - 如何检查 64 位整数的可用性(对于“使用整数”)?
- reactjs - 结合 contextAPI、useState 和 useEffect
- javascript - 如何在一个网页上使用多个编辑器
- schema.org - 不可见元素的 Schema.org 标记
- gradle - 为什么在添加依赖项时tasks.all 运行良好但tasks.matching 不行?
- python - 如何在 docker-compose 文件中为多个容器配置通用 Python 环境?