[Android][Debug] How to check memory leak in Android application esider?
2019. 3. 13. 16:23ㆍPrograming/Debugging
[Solution]
Using com.squareup.leakcanary library.
https://github.com/square/leakcanary
Getting started
In your build.gradle
:
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'
}
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...
}
}
You're good to go! LeakCanary will automatically show a notification when an activity or support fragment memory leak is detected in your debug build.
What's next? You could watch a live investigation then customize LeakCanary to your needs.
[Blogs]
https://medium.com/square-corner-blog/leakcanary-1-6-91fad513b5cf
http://pluu.github.io/blog/android/2015/09/11/leakcanary/
http://dktfrmaster.blogspot.com/2017/01/ctivity-leak-leakcanary.html
https://code.luasoftware.com/tutorials/android/how-to-fix-memory-leaks-in-android/