最近幾天,發現APP OOM了,驚出一身冷汗有沒有。。。
網上一查,studio可以檢查內存,果斷走起
運行APP,切換界面,發現APP內存不斷上漲。泄露有木有。要加班的節奏啊
新版的studio,確實很強大,可以檢測內存泄露問題,
步驟:
概念:內存泄露就是說:退出界面,本該通過GC回收的,因爲存在某種引用而不能回收。運行studio會發現,當切換某個界面的時候,內存不會降到原來水平,一直有殘留,這肯定有泄漏了。
反覆的點擊切換有泄漏的界面,。點擊dump java heap 按鍵,studio就開始工作了
結束後會生成prof文件,打開
內存情況一目瞭然。但還是不大隻能,後來使用的
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);
}
}