Profiler内存检测和使用小结
标签:LeakCanary
,Profiler
1.
Profiler
Android Studio内置的Profiler
已经可以很好的帮助我们进行内存检测和优化了,可以很直观看到CPU
、内存
、网络
的变化,但是有时候简单看看是看不出来内存泄漏的,需要知道具体怎么去分析
参考
2. LeakCanary
这个内存辅助分析工具就登场了
易于集成,自动检测出内存泄漏,十分好用
// 第一小步:
dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.3'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.3'
// Optional, if you use support library fragments:
debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.3'
}
// 第二小步:
public class MyApp 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...
}
}