App Performance analysis tools

Source: Internet
Author: User

This article is basically translated from Facebook engineer's article
The speed up your app also added some of its own content.

The following topics will be introduced

    • Systrace
    • Traceview
    • Memory Profiling
    • Allocation Tracker
    • GPU Profiling
    • Hierarchy Viewer
    • Overdraw
    • Alpha
    • Hardware acceleration

Systrace

Systrace functions can be found in the as DDMS, but not very stable, so this is only the command-line mode.
In addition Systrace can only analyze the general situation, can not locate the problem location, not too interested friends can jump directly to the second section TraceView.
Enter the Sdk/platform-tools/systrace directory first in the terminal (my environment is Mac)
cd /Users/apple/Documents/Android/sdk/platform-tools/systrace

Then the command to execute Systrace is
python systrace.py --time=10 -a com.duotin.fm -o mynewtrace.html sched gfx view wm
–time=10 means record 10 seconds
For more information, please see the official website
Finish opening the generated mynewtrace.html file
Rendering such an interface

Click the triangle Alert icon in the first column or the circular alert icon in the second column to see the alert details, and click the effect as follows

Triangle alert icon, each icon represents a warning, click to view the alert details.

Circular alert icons, each icon representing a frame, showing red or yellow indicates that the frame's time has exceeded 16.6 millisecond per frame, causing the interface to lose frame. Click to view all alerts for this frame.
Press the "M" shortcut key to highlight the currently selected frame. "A" and "D" are shifted left and right, respectively, "W" zooms in, "S" shrinks.
In addition, click on each color block to see the details of each color block.
Click on the first alert:inflation during ListView recycling
Show Details

It can be seen that the execution time of the inflation during ListView recycling is 32ms (far more than the 16ms limit), a total of 5 item, with an average of 6ms per item. You can view the details of each method by tapping the color block within the frame range.

We then click on a circular alert icon and highlight

The top shows the frame takes 19ms, click on the alert at the bottom right, and the scheduled delay is displayed.
Scheduled delay means that the CPU is told to perform a task, but the CPU is too busy and the task is delayed.
Click on one of the following color blocks to display the following

Wall duration refers to the time-consuming method from start to finish that this color block represents
CPU duration refers to the CPU's execution time
You can see that the CPU duration is only 4ms, but wall duration has 18ms.
The delay is so serious, let's see why.
Above the selected color block, we see four CPUs are filled with color blocks, indicating that at this time 4 CPUs have a job, very busy.
We have selected a CPU color block

You can see that CPU-intensive applications are Com.udinic.keepbusyapp
Well, the introduction of Systrace to this end, although some did not say, but systrace really can only look at an overview.
Wait, add a tip, click on the alerts in the right column, you can see all the alerts.

Through this picture, I analyze
Inefficient view has the highest number of alpha usage. Inflation during ListView recycling has the longest impact time and takes up to 52ms

Traceview

TraceView's ability to analyze app performance at the methodological level is very powerful.
Can be started by, command line or GUI, I use the GUI to boot, click as Android Device Monitor, click on the Devices column under the start Method profilling, in the dialog box select, (I chose the trace Based Profilling, said real-time analysis, will be slow, but the analysis results in detail), the operation of the app, the end of the analysis, click on the same icon. For more information, please visit our website:
Look at the interface first.

Column Name meaning
Name Method name, each method has a different color.
Inclusive CPU Time This method consumes CPU time, inclusive refers to the method that includes the call
Exclusive CPU Time The time that this method consumes the CPU, Exclusive refers to methods that do not include calls
Inclusive/exclusive Real Time Real time refers to how long it takes from start to finish, with the wall duration in Systrace a meaning.
Calls+recursion How many times this method has been called + how many times it is called recursively
Calls/total The total number of times the child method was called by this parent method/child method was called

When you click the parent or child method under an entry, the entry for that method is skipped.
To find out how to best affect performance, you can click the exclusive CPU time column to find out the longest-consuming methods. If it is an applied method, it is not optimal to look directly at this method. If it is a system method, go back to the application method by looking at its parent method.
And looking at the sub-method, you can see exactly what this method does.
If you want to find the reason for the UI lag, you can start with GetView (), View#ondraw (), and so on.

When the method executes, it takes up CPU, so too much execution time can cause UI rendering to be delayed, so the application is not fluent. The GC also takes up Cpu,as and provides the tools to view the GC:

Memory Profiling

Click on Android Monitor in as and select Memory | CPU column, the interface is as follows

, a small amount of memory drops is generally the case with GC.
Clicking on the left heap dump will generate a snapshot of all objects in memory.

Column Name meaning
Total Counts The number of objects in this class in memory
Heap Count The number of objects of this class in the heap, the upper-left corner of the app heap or zygote heap can be selected
Sizeof Shallow Size occupied by a single object
Shallow Size Shallow Size occupied by all objects
Retained Size All objects occupy retained Size, which is the memory freed after GC
Instance A specific object of the class
Reference Tree Refer to the parent object of this object, click the parent object, expand the parent object of these parent objects

What is shallow size and retained size

Select a row, click on the right side of a instance, you can see below the refrence tree interface

It can be seen in the diagram that a instance of memoryactivity is referenced in Listenermanager. If Memoryactivity is not already in the activity stack, such a reference is a memory leak. Another tool to check for memory leaks is leakcanary
By looking at retained size and reference Tree, we can know which objects are consuming more memory, referencing relationships among objects, and then analyze whether data structures can be optimized and referential relationships reduced to reduce memory consumption and GC frequency.

Allocation Tracker

Memory | Another button on the left side of the CPU column allocation tracker is also used to analyze memory consumption. Click once to start recording, click again to stop recording.
Click the pie chart at the top left of the results page.
You can select group by Allocator, which is divided by object.

or group by method

Allocator the bottom of the pie chart the outermost is the specific class, the internal is the package name. The figure shows the size or number of memory used by the package or object, the larger the area, the more occupied or the number. Select Size to view the most memory-intensive objects, and select the objects that count can view and the largest number. The former we can try to optimize the class, the latter we can try to create an object pool to use objects.

From the group by method, it can be seen that the Decode method occupies a total memory of 10.91M, it is possible that there are too many new objects in the method, can be optimized in the past.

Tips for Memory:
1. Enums Enums is much larger than int, and has a replacement scheme @intdef, so in some cases, for example, you need to force a specified type, int will save more memory
2. Automatic boxing automatic boxing refers to the automatic conversion from the basic type to the object form (such as int to integer), because the basic type uses more scenes and times, so you need to avoid using its automatic boxing form.
3. HashMap vs Arraymap/sparse*array if we need to use int as the value of the map, you can use Sparseintarray, which saves more memory than using HASHMAP to automatically boxing int. If you want to use object as the map key, in addition to HashMap, you can also consider using Arraymap, functions and HashMap, but more memory, click to understand the principle. Although time performance is better than HashMap, they are almost as fast to use unless you want to store more than 1000 objects.
4. Note that the context object is most likely to cause a memory leak because the context is used more in development. Activity itself is a heavy object, in order to avoid memory leaks, you can wear applicationcontext words, do not preach activity.
5. Avoid non-static inner class non-static inner classes implicitly hold references to external classes, so if an external class is no longer required, but the inner class is still in use, a memory leak is created. In particular, activity classes are defined as static when defining internal classes.

GPU Profiling

First, on the Mobile Developer Options page, click GPU Rendering Mode Analysis (profile GPU rendering), select "in adb shell Dumpsys gfxinfo" and select the GPU column in the Android Monitor interface of AS. Make sure that the pause button on the top left is not selected, and as will begin to display the GPU condition according to the selected package name. Each line represents a frame in the UI rendering, and different colors represent different drawing stages.

    • Draw (blue) executes the View#ondraw () method. The work at this stage is to create displaylist objects that will later be converted to OpenGL commands and transferred to the GPU. If the blue is longer, it is generally because of a more complex view, or a short period of time invalidate more view
    • Prepare (purple) Lollipop is the stage that is introduced to speed up UI rendering and execution in thread renderthread. The task at this stage is to convert the display lists from the first step to the OpenGL command and transfer it to the GPU. The UI thread will continue to process the next frame at this point. The time it takes for the UI thread to deliver the required resources to Renderthread is also documented during this phase. When there is a lot of resources to pass, such as many/very heavy display lists, this phase takes more time.
    • Process (red) handles the display lists, producing OpenGL commands, more or more complex display lists, which can make this phase time-consuming, because many of the view will be redraw. View is redraw the case has invalidate or previously covered view is now removed.
    • Execute (yellow) sends the OpenGL command to the GPU, which is a blocking method because the CPU sends the OpenGL command to the GPU through buffer and returns an empty buffer when processing is complete. The number of buffer is limited, so when the GPU is busy and buffer is exhausted, the CPU needs to wait for the GPU to return an empty buffer before it can continue sending OpenGL commands. So if this stage is more time-consuming, it is generally because of the complex view being drawn.

In the marshmallow version, added more colors

Half of the translation, to be continued

App Performance analysis tools

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.