Android studio使用LeakCanary来测试内存溢出问题

Posted by PanMin on August 21, 2015

使用方法

因为不想让这样的检查在正式给用户的 release 版本中也进行,所以在 dependencies 里添加

dependencies {    
      compile fileTree(dir: 'libs', include: ['*.jar'])    
      compile 'com.android.support:support-v13:+'    
      debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'    
      releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
}

接下来,在你的应用里写一个自定义 Application ,并在其中“安装” RefWatcher

public class AppApplication extends Application {    
      private RefWatcher mRefWatcher;    
      @Override    
      public void onCreate() {        
           super.onCreate();        
           mRefWatcher = LeakCanary.install(this);    
      }
}

记得把它作为 android:name 配到 AndroidManifest.xmlApplication 节点下。