最近几天,发现APP OOM了,惊出一身冷汗有没有。。。
网上一查,studio可以检查内存,果断走起
运行APP,切换界面,发现APP内存不断上涨。泄露有木有。要加班的节奏啊
新版的studio,确实很强大,可以检测内存泄露问题,
步骤:
概念:内存泄露就是说:退出界面,本该通过GC回收的,因为存在某种引用而不能回收。运行studio会发现,当切换某个界面的时候,内存不会降到原来水平,一直有残留,这肯定有泄漏了。
反复的点击切换有泄漏的界面,。点击dump java heap 按键,studio就开始工作了
结束后会生成prof文件,打开
内存情况一目了然。但还是不大只能,后来使用的
LeakCanary
很简单好用
GitHub地址:https://github.com/square/leakcanary
In your build.gradle
:
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'
}
In your Application
class:
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
LeakCanary.install(this);
}
}