使用AndroidStudio提供的Android Profiler工具和mat进行内存泄漏分析
利用 Android Studio 查看内存
准备
1)Android 完整代码
2)真机
1. Profile APP
打开 Android Studio,在右上角,找到 Profile 'app' 的 icon。如果手机上不能自动打开,则要手动打开 APP。完成后,会看到如下图所示的 相关性能图示
APP内切换相关页面,可以看到相关性能的动态变化
2. Dump Java Heap
选择"Memory" -> 点击 “Force Garbage Collection” -> 点击“Dump Java Heap”
3. 分析
dump 完成后,点击左侧的 Heap Dump -> 在 Heap Dump 的下拉框后面选择“app heap” 及“Arrange by package”
--------------------------------- 以下未验证 ---------------------------------
-
-
当堆栈信息获取完成后,会弹出如下框,我们将排序方式选为
Arrange by package(好定位我们自己的代码),找到我们的代码后我们真的发现,应该已经被回收了的LoginActivity还占用这内存。但是为什么没有被销毁,还有那些对象引用着他。这时我们就需要点击位置为3的地方,导出.hprof文件进行具体分析了
image.png
-
导出以后我们会得到
1.hprof文件,但是这个不是mat工具用到的标准文件。我们需要使用sdk自带的hprof-conv.exe(platform-tools文件夹下) 工具进行转换,转换以后我们就得到了1_mat.hprof文件
-
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
try {
//获得 所有属性 getField->public 以及父类
Field mCurRootViewField = InputMethodManager.class.getDeclaredField("mCurRootView");
//设置允许访问权限
mCurRootViewField.setAccessible(true);
// 对象
Object mCurRootView = mCurRootViewField.get(im);
if (null != mCurRootView){
Context context = ((View) mCurRootView).getContext();
if (context == this){
//破怪gc 引用链
mCurRootViewField.set(im,null);
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}