Android Development Performance Optimization Case (2) app Caton optimization

Foreword

Performance Optimization of this series of articles:
1, by way of explanation easy to understand, explain the practical value of a technology
2, a detailed written source tracking, source capture, structure drawing class, try to explain in detail the principles of the exploration process
3 providing the Demo Github project can run, but I have provided the code, provide more ideas, better ideas, please cv appropriate,
4, finishing a set of precautions during the operation principle of the process of exploring some pit, or in the demo
5, FIG using gif, most intuitive operating results show demo **

If you think the details are too thin, you can skip to see the conclusion.
Limited capacity, should the description found inappropriate, welcome messages criticism.

 

Connect an article: start speed optimization

Body outline

  • DDMS
  • systrace
  • TraceView
  • About overdraw

text

DDMS

DDMS stands for Dalvik Debug Monitor Service, the Android development environment Dalvik [virtual machine] Debug Monitor Service

Previously used eclipsetime, there is a direct entrance to open the DDMS, but since the use of AndroidStudio, the entrance did not .... but actually inside the SDK directory is still there.

After opening DDMS:

Specifically dim later say.

systrace

systrace is a command sdk, and it is written in python, was using python2.7, but later updated the python 3.0 Google has not updated this command, leads us now to use systrace command, only with python 2.7 version, under normal circumstances, with the latest 2.7 version of 2.7.16 on the line, official website download.

So where systrace command?

premise

To use it, first of all we want to install a good python2.7.16, and then configure the environment variables, until we can properly use the python command (

systrace is our crawler to index data on an android device over a period of time, I understood as: log equipment operation, but this is not a text log, but a html file, you need to use the Google browser  chrome: // tracing / plugin opens. Specific steps are as follows:
1. Open the CMD, enter systrace directory:

2. Enter  python systrace.py -b 32768 -t 5 -o mytrace.html wm gfx input view sched freq, then Enter
to explain the string of commands 本文不做systrace命令的详解,这些东西都是死命令,百度即可( ):

  • python Python script to be executed
  • systrace.py Screenplay name
  • -b Set the buffer size
  • -t Crawl 5 seconds log
  • -o mytrace.html output to a file within this
  • wm Within WindowManager log contains information windowManager
  • gfx Graphics log contains information graphics rendering
  • input Input log contains information input device
  • view View log information included in the System View System
  • sched Scheduling CPU scheduling information includes a CPU log
  • freq CPU frequency log contains information

Here is a pit:

On some real machine, for example vivo X7, it will generate a htmlfile failed, somehow, I changed the simulator, just fine, yet the real machine testing other models.

NetEase mumu I use the simulator to experiment, I get the following results:

 

3. After obtaining the file, open the Google browser: in the address bar  chrome://tracing/ and then load just double-click the file you :( or html file)

4, where we get a lot of performance indicators, including the image above the red mark font usage of CPU, multi-core CPU scheduling situation, UI main thread, the thread rendering and so on, but we develop the application layer to solve the main problem is the app Caton generally just need to focus on  the main UI thread掉帧情况 can be in accordance with the following figure:

Detailed look at this 带圈的F:

  • The entire coordinates, the horizontal axis is time, the time scale increases from left to right, on behalf of the indicators over time
  • 带圈的F : Green, yellow and red. Where green indicates normal draw, we do not need to care, concern is yellow and red, especially red.
  • Mouse click on a red F, and then press the keyboard Gkeys, there will be 红色的竖线representatives of a red line between each two long (most of the phone's screen refresh rate is still 60, so each drawing about 16.67MS), this F the reason is red, because this is a long drawn the UI far more than one, if the UI can not be completed within a period of, will result in dropped frames, once dropped frames, under the user's perception is that Caton .
  • Look:

.

Use the mouse to drag and drop, you can see through a graphical interface that long time it takes to draw: to116.868ms

  • In the following Alert column found suspected 掉帧culprit

 

Here it reflects our bitmapview of uploaded led off the frame.
We continue to expand the next two arrows, you can see:

English description here, it is Google engineer to give us advice I probably translate this passage:

The first paragraph descriptionmeans:修改/新绘制的位图必须上传到GPU。因为如果上传的总像素量很大,这是很昂贵的,所以每帧减少这个动画/上下文中位图的波动量。

The second paragraph descriptionmeans:生成这个帧的工作被重新调度了几毫秒,这是jank的功劳。确保UI线程上的代码不会阻塞其他线程上的工作,并且后台线程(例如网络或位图加载)在android.os上运行。进程#THREAD_PRIORITY_BACKGROUND或更低,因此它们不太可能中断UI线程。这些后台线程应该在内核进程的调度部分以130或更高的优先级出现。

In general, that is Bitmap的使用不当导致掉帧, the solution is probably: Bitmap too big to be cut into the right size or background thread to load
As for more specific cases dropped frames other solutions, it is necessary to investigate the case according to the specific encounter the data.

About Trace.beginSection and Trace.endSection

It comes with two api is androidSdk, the role is to systrace plus tag, add a tag, it will reflect the implementation include between our two api piece of code in the systrace graphics.
Simply put, you are around a piece of code, plus Trace.beginSection and Trace.endSection, like this:

So, you will find this on systrace graphics.

Visible, time-consuming implementation of our code, etc. can be reflected in systracethe graphic, click on the red box above the area, it will be in systracefound in the bottom of the screen:

If you add a tag code execution takes more than a long (16.67MS), then the piece of code that caused the main UI thread out of frame, the user is likely to feel Caton.

Here is a pit

If you add on top of the trace.beginSection and endSection, you still do not see in the graphic tag set up your own, then check your systrace command, is not no increase -a [app package name]

Be Conclusion

The above example, I use the app when caught cold start systrace, so here dropped frames, with the reflection of a cold start writing the code in question. Note that grab systrace time not too long, must then operate app after systrace started.

After the discovery of dropped frames, looking alert can see Google give our app optimization suggestions direction, although not completely solve the problem, but at least determine a general direction , probably know what a piece of code out of the question.


TraceView

Code added in the app  Debug.startMethodTracing("/sdCard/zhouzhou");, and Debug.stopMethodTracing();then run the app, the above code snippet can be performed to ensure that two code embodied. For example, like this:

Crater

The above code, if you add a run after a direct throw an exception, check that you do not have permission to add this:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Next, find the file, exported to a computer:

Then we first said we should use the DDMS, first open the DDMS, File- openFile open the file just .trace

This contains all method calls this code.

Table shows

There is a table top, a description of each column is as follows:

So many things we can not all concerned, only need to focus on two:

  • Cpu Time/Call Longer average time function execution function; (lengthy function)
  • Call+Recur Calls/Total, Very often a function of the number of calls.

If you find that the above two indicators extraordinary, particularly the number of such calls, each call takes a particularly long time, and also be able to write our own way, then basically you can determine the optimal point.

About overdraw

The 如果屏幕的一片区域,在渲染的过程中,被绘制了太多次,则称为过度绘制concept: .
How to check:

The figure is mumu emulator settings interface, we click 显示过度绘制区域, you will find the interface has changed color:

Color from shallow to deep, deeper, drawing represents a transition will be more serious. Generally have the following colors:

  1. White: no overdraw.
  2. Blue: Overdraw doubled. Pixels drawn twice, to accept, but if the entire interface are blue, then shows there are still drawn to waste, you can save one draw.
  3. Green: Overdraw twice. Trying to optimize.
  4. Light red: Overdraw three times.
  5. Dark: Overdraw four times. Very serious and must be optimized.

How to optimize the transition to draw?

  1. If your code by checking excessive draw and found a large number of complex layout display overdraw, it must be considered  用自定义View自己去绘制.
  2. If your layout xml, there are a lot of nesting, consider 去掉某些 background , because there is no background, UI thread will not do this time to draw
  3. If you have to use the background there is the layout, then consider while meeting business needs 减少一定的层级.

Epilogue

Using the above three tools, performance optimization of our app that must be used. Probably idea is as follows

1. systrace gripping .html files, graphics observation, to find out the approximate location of the frame
2. Trace.beginSection and Trace.endSection repeated estimation, to determine the exact code block
3. traceView gripping .trace logs with open DDMS it is looking very much longer time-consuming function or a function of frequency to determine the exact cause of dropped frames

Three tools need to be used in combination in order to determine our app specifically what went wrong.

Expand topics

This article only gives a general idea, did not give a Demo, which is too often because of the potential performance optimization, we can not list them specifically what happened, only to take place after the results go their own tool detected inference, to demonstrate, to resolve.

Performance optimization is endless, always have to optimize space. This article gives only a rough guide direction, in particular to optimize and to what extent, it all depends on you for self-repair.

The above three tools that can be used for performance optimization, but their role is not only to optimize performance, do not see the wood. For example systrace this thing, I only used to check the above article situation Caton dropped frames, but it can actually view the CPU and CPU scheduling of multi-core allocation, threads can also check the status of the main thread of the switch and so on.

Next:内存抖动和泄漏的优化

Published 56 original articles · won praise 1 · views 2919

Guess you like

Origin blog.csdn.net/chuhe1989/article/details/104478146