基于SnapDragonBoard410C的TraceView 分析

相信大家在调试APP时都是用Logcat去查看整个APP的运行时打印出来的Log,而当我们需要去优化APP的启动速度或者APP卡顿严重时,我们就要使用工具去查看内存和CPU的使用情况了!
这里写图片描述

一.要打开上面的面板,一般有两种方式
1.首先选择跟踪范围,在想要根据的代码片段之间使用以下两句代码.
Debug.startMethodTracing(“hello”);
Debug.stopMethodTracing();
生成的traceview文件会自动放在SDCARD上,没有SDCARD卡会出现异常,所以使用这种方式需要确保应用的AndroidMainfest.xml中的SD卡的读写权限是打开的,其中hello是traceview文件的名字,是然后用adb导出traceview文件。

2.同样是要先打开Android Device Monitor

这里写图片描述
先选择应用进程,然后点击Start Method Profiling(开启方法分析),按钮会变为Stop Method Profiling(停止方法分析),开启方法分析后,对应用的目标页面进行测试操作,测试完毕后停止方法分析,界面会自动跳转到 DDMS 的 trace 分析界面。

两种方式的对比:第一种方式更精确到方法,起点和终点都是自己定,不方便的地方是自己需要添加方法并且要导出文件,第二种方式的优缺点刚好相反。

2、分析方式
下面写一个DEMO,来分别模拟调用次数不多,但每次调用却需要花费很长时间的函数,和自身占用时间不长,但调用却非常频繁的函数。

public class MainActivity extends Activity {
    int count = 0;
    long longCount=-1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Debug.startMethodTracing("hello");
        new Thread(new Runnable() {
            @Override
            public void run() {
                printNum();
            }
        },"printNum_thread").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                calculate();
            }
        },"calculate_thread").start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Debug.stopMethodTracing();
    }

    private void printNum() {
        for (int i = 0; i < 20000; i++) {
            print();
        }
    }
    private void   print(){
        count=count++;
    }
    private  void  calculate(){
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j < 1000; j++) {

                    for (int l = 0; l < 1000; l++) {
                        if(longCount>10){
                            longCount=-10;
                        }
                    }
            }
        }
       Log.e("MainActivity",String.valueOf(longCount));
    }
}

现在来分析一下采集的数据。先看线程面板
这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013763766/article/details/79064415