gpt4 book ai didi

android - com.android.internal.policy.impl.PhoneLayoutInflater 有时会保留在内存中(hprof 转储)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:56 26 4
gpt4 key购买 nike

我正在检查内存,试图通过 hprof 转储查找最终的内存泄漏。

我发现有时当我通过后退按钮离开 Activity 时(这将结束 Activity ), Activity 仍会保留在内存中但它只有两个 GC 根,这似乎不是很“强”虽然。

这是我的 Activity 流程/我点击和测试的方式:

A、B、C 是 Activity 。

1) A -> B -> (回到) A

2) 执行 hprof 转储,结果如下:

B 仍在内存中,B Activity 的 GC 根中唯一的元素是:

com.myapp.android.activity.directory.B

  • com.android.internal.policy.impl.PhoneLayoutInflater 的 mContext

    • android.app.ContextImpl 的 mLayoutInflater [堆栈本地]

      • java.lang.Thread的[局部变量] [Thread] "main"
  • mOuterContext of android.app.ContextImpl [Stack Local]

    • java.lang.Thread的[局部变量] [Thread] "main"

(线程“main”似乎是 UI 线程)

从 A 继续:

3) A -> C -> (回到) A

4) 执行 hprof 转储,结果如下(如预期):

B不在内存中,C不在内存中,只有A

现在我的问题是:这个PhoneLayoutInflater是从哪里来的/为什么从B返回到A的时候还在内存中,但是再到C再返回到A的时候就没有了。

很明显,PhoneLayoutInflater 用于膨胀 View ,我知道它的用途。我只是不明白为什么它会通过主 UI 线程的 GC 根保存在内存中。

当我检查上面列出的 GC 根时

[local variable] of java.lang.Thread [Thread] "main

它将具有以下内容:

  • com.myapp.android.activity.main.A 的 mUiThread [堆栈本地]
    • >....
      • android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper 的 $0 [JNI Global]

我从 A 调用 Activity B 和 C 的方式是通过常规 startActivity(intent)

为什么 Activity A 的主 UI 线程会以某种方式关联并从 Activity B 引用?

最佳答案

这是我最后做的,但我仍然不高兴我使用反射来解决这个问题。有人吗??

public static boolean ReleaseActivityContext(Context context)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Class<?> i = inflater.getClass();

try
{
Field f = i.getSuperclass().getDeclaredField("mContext");
f.setAccessible(true); // prevent IllegalAccessException
f.set(inflater, null); // can cause IllegalAccessException
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}

关于android - com.android.internal.policy.impl.PhoneLayoutInflater 有时会保留在内存中(hprof 转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3149670/

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