gpt4 book ai didi

android - KeyguardManager 内存泄漏

转载 作者:行者123 更新时间:2023-12-04 12:39:53 33 4
gpt4 key购买 nike

我正在使用此代码在 Activity 完成后 LeakCanary 显示以下消息后 DismissKeyguard。如何防止这些泄漏。

keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);

if(Build.VERSION.SDK_INT >= 27) {

setShowWhenLocked(true);
setTurnScreenOn(true);

if (keyguardManager != null) {
keyguardManager.requestDismissKeyguard(this, null);
}
}

LeakCanary 展示

GC Root: Global variable in native code

├─ android.app.KeyguardManager$1 instance
│ Leaking: UNKNOWN
│ Anonymous subclass of com.android.internal.policy.IKeyguardDismissCallback$Stub
│ ↓ KeyguardManager$1.val$activity
│ ~~~~~~~~~~~~
╰→ com.example.myapplication.MainActivity instance
​ Leaking: YES (ObjectWatcher was watching this because com.example.myapplication.MainActivity received Activity#onDestroy() callback and Activity#mDestroyed is true)


最佳答案

leaktrace 表明 native 代码中有一个全局变量,它保留对 KeyguardManager$1 实例的引用,该实例实现了 com.android.internal.policy.IKeyguardDismissCallback$StubKeyguardManager$1 本身持有对 Activity 的引用。

KeyguardManager#requestDismissKeyguard 的实现来源可以在这里找到:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/KeyguardManager.java#574 :

ActivityTaskManager.getService().dismissKeyguard(
activity.getActivityToken(), new IKeyguardDismissCallback.Stub() {
@Override
public void onDismissError() throws RemoteException {
if (callback != null && !activity.isDestroyed()) {
activity.mHandler.post(callback::onDismissError);
}
}
@Override
public void onDismissSucceeded() throws RemoteException {
if (callback != null && !activity.isDestroyed()) {
activity.mHandler.post(callback::onDismissSucceeded);
}
}
@Override
public void onDismissCancelled() throws RemoteException {
if (callback != null && !activity.isDestroyed()) {
activity.mHandler.post(callback::onDismissCancelled);
}
}
}, message);

这是对 ActivityTaskManager 服务进程的进程间调用。设置为在 ActivityTaskManager 服务进程回调时接收结果的 stub 保存对 Activity 的引用。不幸的是, native stub 在内存中的保留时间往往比预期的要长,因为它们依赖于在其他进程中运行的 GC。

这显然是 Android 框架中的一个错误。您应该向 Android 框架提交错误并提供可重现该错误的示例应用。

关于android - KeyguardManager 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60477120/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com