Android 常用第三方库介绍(持续更新中)

目录

一、内存泄露检测之LeakCanary

二、App卡顿检测之BlockCanary

三、图片加载库之Glide


一、内存泄露检测之LeakCanary

github地址:https://github.com/square/leakcanary/

学习地址:https://square.github.io/leakcanary/changelog/#version-22-2020-02-05

使用和原理分析
https://zhuanlan.zhihu.com/p/360944586
https://www.jb51.net/article/208385.htm#_lab2_2_2

关注点

1.LeakCananry自动检测步骤:

  • 检测可能泄漏的对象;
  • 堆快照,生成hprof文件;
  • 分析hprof文件;
  • 对泄漏进行分类。

2.不用主动初始化,因为注册了contentProvider 在onCreate()时做了自动初始化的逻辑。

3.主动监测的实现

  • Activity的生命周期监听:注册 Application.ActivityLifecycleCallbacks ;
  • Fragment的生命周期期监听:同样注册FragmentManager.FragmentLifecycleCallbacks
  • 反射获取 WindowManagerGlobal 的mViews属性
    用到了这个库 https://github.com/square/curtains

4.使用弱引用 来监听需要监听的对象,调动 Runtime.getRuntime.gc() 和 System.runFinalization() 来触发垃圾回收。然后再循环判断保存在队列中弱引用是否为空。为空,则从map中移除掉。最后根据map中剩下的元素数量来决定是否生成hprof文件,调用Debug.dumpHprofData()方法可以生成该文件。最后就是对hprof文件的。

System.runFinalization(); //强制调用已经失去引用的对象的finalize方法

5.有兴趣的话,可以自行了解hprof文件的分析处理过程。

二、App卡顿检测之BlockCanary

github地址:https://github.com/markzhai/AndroidPerformanceMonitor

学习地址:http://blog.zhaiyifan.cn/2016/01/16/BlockCanaryTransparentPerformanceMonitor/

使用和原理分析
http://blog.zhaiyifan.cn/2016/01/16/BlockCanaryTransparentPerformanceMonitor/
https://www.jianshu.com/p/d172aafc3437

关注点:
1.Looper 的loop()方法内在循环从MessageQueue中获取的消息来处理的前后预留了接口,如下图所示,只要通过Looper.getMainLooper().setMessageLogging()来设置自定义的Printer,即可收到回调,用户就可以在回调中做一些cpu 、耗时等信息的收集。

public static void loop() {
    ...

    for (;;) {
        ...

        // This must be in a local variable, in case a UI event sets the logger
        Printer logging = me.mLogging;
        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);
        }

        msg.target.dispatchMessage(msg);

        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
        }

        ...
    }
}

三、图片加载库之Glide

github地址:https://github.com/bumptech/glide

学习地址 :https://muyangmin.github.io/glide-docs-cn/

与其他图片加载库的对比
https://www.jianshu.com/p/97994c9693f9
https://blog.csdn.net/github_33304260/article/details/70213300

Glide源码分析
https://cloud.tencent.com/developer/article/1485133

你可能感兴趣的:(Android 常用第三方库介绍(持续更新中))