MAT
,Memory Monitor
(属于AndroidMonitor中一个模块),HeapTool
(查看堆信息),Allaction Tracking
,LeakCanary
Lint
工具在内存图中点击途中箭头的部分,启动追踪,再次点击就是停止追踪,随后自动生成一个alloc结尾的文件,这个文件就记录了这次追踪到的所有数据,然后会在右上角打开一个窗口。展示和第一种方式有点区别,各有所长,他有两种展现方式。
红框中:
Group by Method:用方法来分类我们的内存分配,默认会以Group by Method来组织
Group by Allocator:用内存分配器来分类我们的内存分配
我们用 Group by Allocator的方式来查看一下:
可以看到我们自己包中,每一个类的内存分配次数和分配的大小。如果我们想看内存分配的实际在源码中发生的地方,可以选择需要跳转的对象,点击该按钮就能发现我们的源码。
想看某个图层详细内存分配,则双击速表左键进入下一图层:
内存分配情况
通过[Layout方式查看更直观:
Getting started In your build.gradle: dependencies { debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.2' releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.2' } In your Application class: public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. return; } LeakCanary.install(this); // Normal app init code... } }
public class CommonUtils { private static CommonUtils instance; private Context context; private CommonUtils(Context context) { this.context = context; } public static CommonUtils getInstance(Context context) { if (instance == null) { instance = new CommonUtils(context); } return instance; } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CommonUtils commonUtils = CommonUtils.getInstance(this); }
LeakCanary
本质上还是用命令控制生成hprof
文件分析检查内存泄漏。
Android Studio
很方便,很好用,你可以试试点击AS菜单栏上面的Analyze
选项,然后点击Inspection
Scope
,然后选择你需要检测的范围(比如整个项目),然后AS会自动弹出下图所示的面板
可能出现内存泄漏的类:
检测资源文件是否有没有用到的资源。
检测常见内存泄露
安全问题SDK版本安全问题
是否有费的代码没有用到
代码的规范---甚至驼峰命名法也会检测
自动生成的罗列出来
没用的导包
可能的bug
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。