最近有个泄漏很诡异,来自华为 P9
泄漏日志:
static android.rms.iaware.FastgrabCongigReader.mFastgrabCongigReader
references android.rms.iaware.FastgrabCongigReader.mContext
...
借用一张泄漏图:
image
LoginActivity设置给了,导致LoginActivity被静态变量持有无法释放
- 分析一下原因,无外乎静态成员变量mFastgrabConfigReader持有的mContext ,这里引用了上下文,导致Activity无法释放掉。只要释放掉就可以了
- 但是,找到对应类发现FastgrabCongigReader并没有用到,泄漏的Activity中也没有mFastgrabCongigReader相关代码,猜测是系统的锅。
- 解决:在Activity销毁回调中,通过反射进行置空即可。
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();
}
}
网友评论