StudioAndroid

使用AndroidStudio提供的Android Profil

2018-05-30  本文已影响2923人  河里的枇杷树

废话不多说直接说流程

  1. 给项目中集成LeakCanary工具进行内存泄漏检测。发现有内存泄漏后该工具会进行提示

  2. 有内存泄露后我们需要使用as的profiler工具进行分析并获取到.hprof文件,步骤如下

转换mat标准文件
命令:hprof-conv -z src dst
例如:hprof-conv -z 1.hprof 1_mat.hprof
image.png
        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();
        }
上一篇 下一篇

猜你喜欢

热点阅读