内存泄露检测对于我个人而言,是对我自己写的代码一次检查,可以检查代码哪些地方写得不对或者错误。
leakcanary 比较好用,git官网:https://github.com/square/leakcanary
1.在app build.gradle添加dependencies
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
2.在applocation 里添加代码
private void leakCanary(){
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);
3.接着run自己app就行,把自己的app安装到测试手机上全部页面跑一下,多操作几次,
内存溢出信息会以通知的形式展示在通知栏上,再逐条更改。
内存溢出情景:(长期添加)
1.设置的静态变量,导致内存溢出。
在a activity里写了一个静态变量,在b中调用,导致的内存泄漏。android变量的保存可以通过sp和application
来暂时存储,可以避免发送内存泄露。