zoukankan      html  css  js  c++  java
  • 内存泄漏监测-LeakCanary

    内存泄漏监测方法之使用LeakCanary

    LeakCanary出处:

    github:https://github.com/square/leakcanary/issues

    square 公司

    这个公司大名不知道,但你一定知道OKHTTP.

    还有很多开源看下图

    使用方法:

    android studio 

    添加依赖:

     dependencies {
       debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
       releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
       testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
     }

    eclipse:自行下包

    使用方法很简单,在application install就可以了。

    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            LeakCanary.install(this);
        }
    }

    如如应用存在多个进程,application onreate 会调用多次。这么写是不是有问题。Install多次。

    既然是大厂出的东西,他们是已经考虑到了。

    看源码:

    /**
       * Creates a {@link RefWatcher} that reports results to the provided service, and starts watching
       * activity references (on ICS+).
       */
      public static RefWatcher install(Application application,
          Class<? extends AbstractAnalysisResultService> listenerServiceClass,
          ExcludedRefs excludedRefs) {
        //判断是否在Analyzer进程里
        if (isInAnalyzerProcess(application)) {
          return RefWatcher.DISABLED;
        }
        enableDisplayLeakActivity(application);
        HeapDump.Listener heapDumpListener =
            new ServiceHeapDumpListener(application, listenerServiceClass);
        RefWatcher refWatcher = androidWatcher(application, heapDumpListener, excludedRefs);
        ActivityRefWatcher.installOnIcsPlus(application, refWatcher);
        return refWatcher;
      }

    当然也可以:

    if (LeakCanary.isInAnalyzerProcess(this)) {
      return;
    }

    这么判断。

    通过以上操作,功能已经可以使用了,十分傻瓜。

    上手试试加以下加代码:

    public class LoginActivity extends Activity {
        static Thread t;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            t = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
    
                    }
                }
            });
            t.start();
    
            finish();
        }

    结果如下:

  • 相关阅读:
    前后台分离--概念相关
    dubbo概述
    memcache
    分布式系统事务
    2018书单索引
    Lucene原理之概念
    java 通用对象排序
    LightOJ1214 Large Division —— 大数求模
    LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数
    LightOJ1245 Harmonic Number (II) —— 规律
  • 原文地址:https://www.cnblogs.com/mamamia/p/7867152.html
Copyright © 2011-2022 走看看