bug

FastgrabCongigReader 华为P9 内存泄漏

Posted by Jfson on 2017-09-20

最近有个泄漏很诡异,来自华为 P9

泄漏日志:

1
2
3
static android.rms.iaware.FastgrabCongigReader.mFastgrabCongigReader
references android.rms.iaware.FastgrabCongigReader.mContext
...

借用一张泄漏图:
image
LoginActivity设置给了,导致LoginActivity被静态变量持有无法释放

  • 分析一下原因,无外乎静态成员变量mFastgrabConfigReader持有的mContext ,这里引用了上下文,导致Activity无法释放掉。只要释放掉就可以了
  • 但是,找到对应类发现FastgrabCongigReader并没有用到,泄漏的Activity中也没有mFastgrabCongigReader相关代码,猜测是系统的锅。
  • 解决:在Activity销毁回调中,通过反射进行置空即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void fixMemoryLeak() {
if (!"HUAWEI".equalsIgnoreCase(Build.MANUFACTURER)) {
return;
}
try {
Class<?> FastgrabConfigReaderClass = Class.forName("android.rms.iaware.FastgrabConfigReader");
Field mFastgrabConfigReader = FastgrabConfigReaderClass.getDeclaredField("mFastgrabConfigReader");
mFastgrabConfigReader.setAccessible(true);
Object fastgrabConfigReader = mFastgrabConfigReader.get(FastgrabConfigReaderClass);
Field contextField = FastgrabConfigReaderClass.getDeclaredField("mContext");
contextField.setAccessible(true);
if (contextField.get(fastgrabConfigReader) == this) {
contextField.set(fastgrabConfigReader, null);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}

pv UV: