Android 解决InputMethodManager 内存泄露问题

Android 解决InputMethodManager 内存泄露问题,第1张

在android 11的 InputMethodManager的源码中,查看 windowDismissed(),如下

/**
 * An empty method only to avoid crashes of apps that call this method via reflection and do not
 * handle {@link NoSuchMethodException} in a graceful manner.
 *
 * @deprecated This is an empty method.  No framework method must call this method.
 * @hide
 */
public void windowDismissed(IBinder appWindowToken) {
    // Intentionally empty.
    //
    // It seems that some applications call this method via reflection to null clear the
    // following fields that used to exist in InputMethodManager:
    //  * InputMethodManager#mCurRootView
    //  * InputMethodManager#mServedView
    //  * InputMethodManager#mNextServedView
    // so that these objects can be garbage-collected when an Activity gets dismissed.
    //
    // It is indeed true that older versions of InputMethodManager had issues that prevented
    // these fields from being null-cleared when it should have been, but the understanding of
    // the engineering team is that all known issues have already been fixed as of Android 10.
    //
    // For older devices, developers can work around the object leaks by using
    // androidx.activity.ComponentActivity.
    // See https://issuetracker.google.com/u/1/issues/37122102 for details.
    //
    // If you believe InputMethodManager is leaking objects in API 24 or any later version,
    // please file a bug at https://issuetracker.google.com/issues/new?component=192705.
}

/*
* 以下三个字段,在低于 android 10 的版本中是都存在的;之后,有变更。
* 低于 android 10,当Activity dismissed 时,可以通过反射,将它们置null,使它们不再持有该Activity中View的引用,防止内存泄露。
* InputMethodManager#mCurRootView
* InputMethodManager#mServedView
* InputMethodManager#mNextServedView
*
* android 10之后修复了所有已知问题。
* 对于较老的版本,建议使用 androidx.activity.ComponentActivity 及其子类。
* issues/37122102,说明 android N/7.0 (api 24),就开始了修复。
*
* android 11 source code:http://aospxref.com/android-11.0.0_r21/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
* android 10 source code:http://aospxref.com/android-10.0.0_r47/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
*/

解决方案:对于低于android10,且非 ComponentActivity的context,反射获取InputMethodManager对象中的 mCurRootViewmServedViewmNextServedView这三个属性,转换为View类型后,判断view的context等于要释放的Activity的context时,将这个属性置为null。

fun fixMemoryLeak(context: Context?) {
    try {
        context ?: return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return // android 10 已修复
        if (context is ComponentActivity) return // androidx.activity.ComponentActivity 已修复
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager ?: return
        imm.javaClass.declaredFields.filter {
            it.name == "mCurRootView" || it.name == "mServedView" || it.name == "mNextServedView"
        }.forEach { filed ->
            val origin = filed.isAccessible
            if (!origin) {
                filed.isAccessible = true
            }
            (filed.get(imm) as? View)?.takeIf { it.context == context }?.also {
                filed.set(imm, null)
            }
            filed.isAccessible = origin
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/798062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存