为了能够简单迅速的发现内存泄漏,Square公司基于MAT开源了LeakCanary
- debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
- releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
- //在自己的Application中添加如下代码
- public static RefWatcher getRefWatcher(Context context) {
- App application = (App) context.getApplicationContext();
- return application.refWatcher;
- }
- //在自己的Application中添加如下代码
- private RefWatcher refWatcher;
- @Override
- public void onCreate() {
- super.onCreate();
- //在自己的Application中添加如下代码
- refWatcher = LeakCanary.install(this);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //在自己的应用初始Activity中加入如下两行代码
- RefWatcher refWatcher = MyApp.getRefWatcher(this);
- refWatcher.watch(this);
- }