BlockCanary原理分析

概述

BlockCanary是Android平台上的一个轻量的,非侵入式的性能监控组件,可以在使用应用的时候检测主线程上的各种卡顿问题,并可通过组件提供的各种信息分析出原因并进行修复。

使用

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

Step1. 配置build.gradle

dependencies {    // most often used way, enable notification to notify block event    implementation 'com.github.markzhai:blockcanary-android:1.5.0'
    // this way you only enable BlockCanary in debug package    // debugImplementation 'com.github.markzhai:blockcanary-android:1.5.0'    // releaseImplementation 'com.github.markzhai:blockcanary-no-op:1.5.0'}

Step2. 在Application中注册

public class DemoApplication extends Application {    @Override    public void onCreate() {        // ...        // Do it on main process        BlockCanary.install(this, new BlockCanaryContext()).start();    }}

Step3. 检测结果

原理

在Android中,应用的卡顿,主要是在主线程阻塞导致的。Looper是主线程的消息调度者,所以以它为突破点。

Looper#loop()

在Looper的loop方法中,有一个Printer,它在每个Message处理的前后被调用,而如果主线程卡住了,就是 dispatchMessage里卡住了。

public static void loop() {    // ....
    for (;;) {        // ...
        // This must be in a local variable, in case a UI event sets the logger        final 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);        }       // ...    }}

获取主线程的Looper

因为Looper在每个线程最多只有一个实例,所以只要获取到主线程的Looper,就可以设置一个自定义的Printer对象到里面。

Looper mainLooper = Looper.getMainLooper();

创建自定义Printer

在Printer的println方法去计算主线程一条Message处理的时长,当时长超过设定的阈值时就判定是卡顿了。

...@Overridepublic void println(String x) {    if (!mStartedPrinting) {        mStartTimeMillis = System.currentTimeMillis();        mStartThreadTimeMillis = SystemClock.currentThreadTimeMillis();        mStartedPrinting = true;    } else {        final long endTime = System.currentTimeMillis();        mStartedPrinting = false;        if (isBlock(endTime)) {            notifyBlockEvent(endTime);        }    }}
private boolean isBlock(long endTime) {    return endTime - mStartTimeMillis > mBlockThresholdMillis;}...

设置自定义Printer到主线程Looper

Looper.getMainLooper().setMessageLogging(mainLooperPrinter);

流程图

参考链接

  1. [BlockCanary — 轻松找出Android App界面卡顿元凶]https://blog.zhaiyifan.cn/2016/01/16/BlockCanaryTransparentPerformanceMonitor/

文章来源于互联网:BlockCanary原理分析

下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.dandroid.cn/17071,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?