LeakCanary的用法

为了能够简单迅速的发现内存泄漏,Square公司基于MAT开源了LeakCanary

  1. debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'  
  2. releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'  
2、Application中初始化leakcanary

[java]  view plain  copy
  1. //在自己的Application中添加如下代码  
  2. public static RefWatcher getRefWatcher(Context context) {  
  3.     App application = (App) context.getApplicationContext();  
  4.     return application.refWatcher;  
  5. }  
  6.   
  7. //在自己的Application中添加如下代码  
  8. private RefWatcher refWatcher;  
  9. @Override  
  10. public void onCreate() {  
  11.     super.onCreate();  
  12.     //在自己的Application中添加如下代码  
  13.     refWatcher = LeakCanary.install(this);  
  14. }  
3、Activity中使用

[java]  view plain  copy
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.activity_main);  
  5.     //在自己的应用初始Activity中加入如下两行代码  
  6.     RefWatcher refWatcher = MyApp.getRefWatcher(this);  
  7.     refWatcher.watch(this);  
  8. }  

猜你喜欢

转载自blog.csdn.net/qq_36347817/article/details/80040556