飞屋环游记qvod,windows8 激活,柳慧珠
本文为大家分享了android内存泄漏排查利器,供大家参考,具体内容如下
开源地址:
在 build.gralde 里加上依赖, 然后sync 一下, 添加内容如下
dependencies { .... debugcompile 'com.squareup.leakcanary:leakcanary-android:1.5' releasecompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5' testcompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5' }
省略号代表其他已有内容
在 application类里面将 leakcanary 初始化。。 比如叫myapplication ,如果没有就创建一个,继承 android.app.application。 别忘了在androidmanifest.xml中加上,否则不起作用
public class myapplication 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); // 你的其他代码从下面开始 } }
官方已经有demo了,可以跑跑看。
package com.github.pandafang.leakcanarytest; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.os.systemclock; import android.view.view; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); view button = findviewbyid(r.id.async_task); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startasynctask(); } }); } void startasynctask() { // this async task is an anonymous class and therefore has a hidden reference to the outer // class mainactivity. if the activity gets destroyed before the task finishes (e.g. rotation), // the activity instance will leak. new asynctask<void, void, void>() { @override protected void doinbackground(void... params) { // do some slow work in background systemclock.sleep(20000); return null; } }.execute(); } }
进入主界面按下按钮, 再按返回键退出主界面, 反复几次,leakcanary 就能探测到内存泄漏了。注意要多操作几次,1次的话泄漏规模太小,可能不会探测到。leakcanary 一旦探测到会弹出提示的。
回到桌面,会看到一个leakcanary 的图标,如果有多个app 用到就会有多个leakcanary图标。
点进去就会看到内存泄漏记录
再点进去就可以看到调用栈了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论