- Analyzing UI Performance with Systrace
- In this document
- See also
- Overview
- Generating a Trace
- Tracing on Android 4.3 and higher
- Tracing on Android 4.2 and lower
- Analyzing a Trace
- Inspecting Frames
- Investigating Alerts
- Tracing Application Code
- Debugging with Android Studio
- In this document
- See also
- Run your App in Debug Mode
- Attach the debugger to a running process
- Use the System Log
- Write log messages in your code
- View the system log
- Work with Breakpoints
- View and configure breakpoints
- Debug your app with breakpoints
- Track Object Allocation
- Analyze Runtime Metrics to Optimize your App
- Capture Screenshots and Videos
Analyzing UI Performance with Systrace
In this document
See also
While developing your application, you should check that user interactions are buttery smooth, running at a consistent 60 frames per second. If something goes wrong, and a frame gets dropped, the first step in fixing the problem is understanding what the system is doing.
The Systrace tool allows you to collect and inspect timing information across an entire Android device, which is called a trace. It shows where time and CPU cycles are being spent, displaying what each thread and process is doing at any given time. It also inpects the captured tracing information to highlight problems that it observes, from list item recycling to rendering content, and provide recommendations about how to fix them. This document explains how to navigate the trace files produced by the tool, and use them to analyze the performance of an application’s user interface (UI).
Overview
Systrace helps you analyze how the execution of your application fits into the many running systems on an Android device. It puts together system and application thread execution on a common timeline. In order to analyze your app with Systrace, you first collect a trace log of your app, and the system activity. The generated trace allows you to view highly detailed, interactive reports showing everything happening the system for the traced duration.
Figure 1. An example Systrace, showing 5 seconds of scrolling an app when it is not performing well.
Figure 1. shows a trace captured while scrolling an app that is not rendering smoothly. By default, a zoomed out view of the traced duration is shown. The horizontal axis is time, and trace events are grouped by process, and then by thread on the vertical axis.
The groupings are in the order Kernel, SurfaceFlinger (the android compositor process), followed by apps, each labeled by package name. Each app process contains all of the tracing signals from each thread it contains, including a hierarchy of high level tracing events based on the enabled tracing categories.
Generating a Trace
In order to create a trace of your application, you must perform a few setup steps. First, you must have a device running Android 4.1 (API 16) or higher. Set up the device for debugging, connect it to your development system, and install your application. Some types of trace information, specifically disk activity and kernel work queues, require that you have root access to the device. However, most Systrace log data only requires that the device be enabled for developer debugging.
Systrace traces can be run either from a command line or from a graphical user interface. This guide focuses on using the command line options.
Tracing on Android 4.3 and higher
To run a trace on Android 4.3 and higher devices:
- Make sure the device is connected through a USB cable and is enabled for debugging.
- Run the trace with the options you want, for example:
- On the device, execute any user actions you want be included in the trace.
For more information on the available options for running Systrace, see the Systrace help page.
Tracing on Android 4.2 and lower
To use Systrace effectively with devices running Android 4.2 and lower, you must configure the types of processes you want to trace before running a trace. The tool can gather the following types of process information:
- General system processes such as graphics, audio and input processes (selected using trace category tags).
- Low level system information such as CPU, kernel and disk activity (selected using options).
To set trace tags for Systrace using the command-line:
- Use the —set-tags option:
- Stop and restart the adb shell to enable tracing of these processes.
To set trace tags for Systrace using the device user interface:
- On the device connected for tracing, navigate to: Settings > Developer options > Monitoring > Enable traces.
- Select the categories of processes to be traced and click OK.
Note: The adb shell does not have to be stopped and restarted when selecting trace tags using this method.
After you have configured the category tags for your trace, you can start collecting information for analysis.
To run a trace using the current trace tag settings:
- Make sure the device is connected through a USB cable and is enabled for debugging.
- Run the trace with the low-level system trace options and limits you want, for example:
- On the device, execute any user actions you want be included in the trace.
For more information on the available options for running Systrace, see the Systrace help page.
Analyzing a Trace
After you have generated a trace, open the output html file using a web browser. This section explains how to analyze and interpret the information that the tool produces to find and fix UI performance problems.
Inspecting Frames
Each app that is rendering frames shows a row of frame circles, which are typically colored green. Circles that are colored yellow or red, exceeding the 16.6 millisecond run time limit required to maintain a stable 60 frames per second. Zoom in using the ‘w’ key to see the frames of your application, and look for long-running frames getting in the way of smoothness.
Note: Hit the ‘?’ key, or the button in the top right for help navigating the trace.
Figure 2. Systrace display after zooming in on a long-running frame.
Clicking on one such frame highlights it, focusing only on the work done by the system for that frame. On devices running Android 5.0 (API level 21) or higher, this work is split between the UI Thread and RenderThread. On prior versions, all work in creating a frame is done on the UI Thread.
Click on individual components of the frame to see how long they took to run. Some events, such as performTraversals, describe what the system is doing in that method when you select it. Selecting a frame displays any alerts present in that frame.
Investigating Alerts
Systrace does automatic analysis of the events in the trace, and highlights many performance problems as alerts, suggesting what to do next.
Figure 3. Selecting the problematic frame, an alert is shown identifying a problem.
After you select a slow frame such as the one shown in Figure 3, an alert may be displayed. In the case above, it calls out that the primary problem with the frame is too much work being done inside ListView recycling and rebinding. There are links to the relevant events in the trace, which can be followed to explain more about what the system is doing during this time.
If you see too much work being done on the UI thread, as in this case with this ListView work, you can use Traceview, the app code profiling tool, to investigate exactly what is taking so much time.
Note that you can also find about every alert in the trace by clicking the Alerts tab to the far right of the window. Doing so expands the Alerts panel, where you can see every alert that the tool discovered in your trace, along with an occurrence count.
Figure 4. Clicking the Alert button to the right reveals the alert tab.
The Alerts panel helps you see which problems occur in the trace, and how often they contribute to jank. Think of the alerts panel as a list of bugs to be fixed, often a tiny change or improvement in one area can eliminate an entire class of alerts from your application!
Tracing Application Code
The tracing signals defined by the framework do not have visibility into everything your application is doing, so you may want to add your own. In Android 4.3 (API level 18) and higher, you can use the methods of the Trace class to add signals to your code. This technique can help you see what work your application’s threads are doing at any given time. Tracing begin and end events do add overhead while a trace is being captured, a few microseconds each, but sprinkling in a few per frame, or per worker thread task can go a long way to adding context to a trace of your app.
The following code example shows how to use the Trace class to track execution of an application method, including two nested code blocks within that method.
Note: When you nest trace calls within each other, the endSection() method ends the most recently called beginSection(String) method. This means that a trace started within another trace cannot extend beyond the end of the enclosing trace, so make sure your beginning and ending method calls are properly matched to measure your applications processing.
Note: Traces must begin and end on the same thread. Do not call beginSection(String) on one thread of execution and then attempt to end the trace with a call to endSection() on another thread.
When using application-level tracing with Systrace, you must specify the package name of your application in the user interface or specify the -a or —app= options on the command line. For more information, see the Systrace usage guide.
You should enable app level tracing when profiling your app, even if you have not added signals yourself. Library code can include very useful tracing signals when you enable application-level tracing. The RecyclerView class is a great example of this, providing information about several important stages of work it executes.
Источник
Debugging with Android Studio
In this document
See also
Android Studio enables you to debug apps running on the emulator or on an Android device. With Android Studio, you can:
- Select a device to debug your app on.
- View the system log.
- Set breakpoints in your code.
- Examine variables and evaluate expressions at run time.
- Run the debugging tools from the Android SDK.
- Capture screenshots and videos of your app.
To debug your app, Android Studio builds a debuggable version of your app, connects to a device or to the emulator, installs the app and runs it. The IDE shows the system log while your app is running and provides debugging tools to filter log messages, work with breakpoints, and control the execution flow.
Run your App in Debug Mode
Figure 1. The Choose Device window enables you to select a physical Android device or a virtual device to debug your app.
To run your app in debug mode, you build an APK signed with a debug key and install it on a physical Android device or on the Android emulator. To set up an Android device for development, see Using Hardware Devices. For more information about the emulator provided by the Android SDK, see Using the Emulator.
To debug your app in Android Studio:
- Open your project in Android Studio.
- Click Debug in the toolbar.
- On the Choose Device window, select a hardware device from the list or choose a virtual device.
- Click OK. Your app starts on the selected device.
Figure 1 shows the Choose Device window. The list shows all the Android devices connected to your computer. Select Launch Emulator to use an Android virtual device instead. Click the ellipsis to open the Android Virtual Device Manager.
Android Studio opens the Debug tool window when you debug your app. To open the Debug window manually, click Debug . This window shows threads and variables in the Debugger tab, the device status in the Console tab, and the system log in the Logcat tab. The Debug tool window also provides other debugging tools covered in the following sections.
Figure 2. The Debug tool window in Android Studio showing the current thread and the object tree for a variable.
Attach the debugger to a running process
You don’t always have to restart your app to debug it. To debug an app that you’re already running:
- Click Attach debugger to Android proccess
.
- In the Choose Process window, select the device and app you want to attach the debugger to.
- To open the Debug tool window, click Debug .
Use the System Log
The system log shows system messages while you debug your app. These messages include information from apps running on the device. If you want to use the system log to debug your app, make sure your code writes log messages and prints the stack trace for exceptions while your app is in the development phase.
Write log messages in your code
To write log messages in your code, use the Log class. Log messages help you understand the execution flow by collecting the system debug output while you interact with your app. Log messages can tell you what part of your application failed. For more information about logging, see Reading and Writing Logs.
The following example shows how you might add log messages to determine if previous state information is available when your activity starts:
During development, your code can also catch exceptions and write the stack trace to the system log:
Note: Remove debug log messages and stack trace print calls from your code when you are ready to publish your app. You could do this by setting a DEBUG flag and placing debug log messages inside conditional statements.
View the system log
Both the Android DDMS (Dalvik Debug Monitor Server) and the Debug tool windows show the system log; however, the Android DDMS tool window lets you view only log messages for a particular process. To view the system log on the Android DDMS tool window:
- Start your app as described in Run your App in Debug Mode.
- Click Android
to open the Android DDMS tool window.
- If the system log is empty in the Logcat view, click Restart
.
Figure 4. The system log in the Android DDMS tool window.
The Android DDMS tool window gives you access to some DDMS features from Android Studio. For more information about DDMS, see Using DDMS.
The system log shows messages from Android services and other Android apps. To filter the log messages to view only the ones you are interested in, use the tools in the Android DDMS window:
- To show only log messages for a particular process, select the process in the Devices view and then click Only Show Logcat from Selected Process
. If the Devices view is not available, click Restore Devices View
on the right of the Android DDMS tool window. This button is only visible when you hide the Devices window.
- To filter log messages by log level, select a level under Log Level on the top of the Android DDMS window.
- To show only log messages that contain a particular string, enter the string in the search box and press Enter.
Work with Breakpoints
Breakpoints enable you to pause the execution of your app at a particular line of code, examine variables, evaluate expressions, and continue the execution line by line. Use breakpoints to determine the causes of run-time errors that you can’t fix by looking at your code only. To debug your app using breakpoints:
- Open the source file in which you want to set a breakpoint.
- Locate the line where you want to set a breakpoint and click on it.
- Click on the yellow portion of the side bar to the left of this line, as shown in figure 5.
- Start your app as described in Run your App in Debug Mode.
Android Studio pauses the execution of your app when it reaches the breakpoint. You can then use the tools in the Debug tool window to identify the cause of the error.
Figure 5. A red dot appears next to the line when you set a breakpoint.
View and configure breakpoints
To view all the breakpoints and configure breakpoint settings, click View Breakpoints on the left side of the Debug tool window. The Breakpoints window appears, as shown in figure 6.
Figure 6. The Breakpoints window lists all the current breakpoints and includes behavior settings for each.
The Breakpoints window lets you enable or disable each breakpoint from the list on the left. If a breakpoint is disabled, Android Studio does not pause your app when it hits that breakpoint. Select a breakpoint from the list to configure its settings. You can configure a breakpoint to be disabled at first and have the system enable it after a different breakpoint is hit. You can also configure whether a breakpoint should be disabled after it is hit. To set a breakpoint for any exception, select Exception Breakpoints in the list of breakpoints.
Debug your app with breakpoints
After you set breakpoints in your code, click Rerun to start the app again. When a breakpoint is hit, Android Studio pauses the app and highlights the breakpoint in the source code. The Debug tool window lets you examine variables and control the execution step by step:
To examine the object tree for a variable, expand it in the Variables view. If the Variables view is not visible, click Restore Variables View .
To evaluate an expression at the current execution point, click Evaluate Expression .
To advance to the next line in the code (without entering a method), click Step Over .
To advance to the first line inside a method call, click Step Into .
To advance to the next line outside the current method, click Step Out .
To continue running the app normally, click Resume Program .
Figure 7. The Variables view in the Debug tool window.
Track Object Allocation
Android Studio lets you track objects that are being allocated on the Java heap and see which classes and threads are allocating these objects. This allows you to see the list of objects allocated during a period of interest. This information is valuable for assessing memory usage that can affect application performance.
To track memory allocation of objects:
- Start your app as described in Run Your App in Debug Mode.
- Click Android
to open the Android DDMS tool window.
- On the Android DDMS tool window, select the Devices | logcat tab.
- Select your device from the dropdown list.
- Select your app by its package name from the list of running apps.
- Click Start Allocation Tracking
- Interact with your app on the device.
- Click Stop Allocation Tracking
Android Studio shows the objects that the system allocated with the following information:
- Allocation order
- Allocated class
- Allocation size
- Thread ID
- Allocation method, class, and line number
- Stack trace at the point of allocation
Figure 8. Object allocation tracking in Android Studio.
Analyze Runtime Metrics to Optimize your App
Even if your application does not generate runtime errors, this does not mean it is free of problems. You should also consider the following issues:
- Does your app use memory efficiently?
- Does your app generate unnecessary network traffic?
- What methods should you focus your attention on to improve the performance of your app?
- Does your app behave properly when the user receives a phone call or a message?
The Android Device Monitor is a stand-alone tool with a graphical user interface for serveral Android application debugging and analysis tools, including the Dalvik Debug Monitor Server (DDMS). You can use the Android Device Monitor to analyze memory usage, profile methods, monitor network traffic and simulate incoming calls and messages.
To open the Android Device Monitor from Android Studio, click Monitor on the toolbar. The Android Device Monitor opens in a new window.
For more information about the Android Device Monitor and DDMS, see Device Monitor and Using DDMS.
Capture Screenshots and Videos
Android Studio enables you to capture a screenshot or a short video of the device screen while your app is running. Screenshots and videos are useful as promotional materials for your app, and you can also attach them to bug reports that you send to your development team.
To take a screenshot of your app:
- Start your app as described in Run your App in Debug Mode.
- Click Android
to open the Android DDMS tool window.
- Click Screen Capture
on the left side of the Android DDMS tool window.
- Optional: To add a device frame around your screenshot, enable the Frame screenshot option.
- Click Save.
To take a video recording of your app:
- Start your app as described in Run your App in Debug Mode.
- Click Android
to open the Android DDMS tool window.
- Click Screen Record
on the left side of the Android DDMS tool window.
- Click Start Recording.
- Interact with your app.
- Click Stop Recording.
- Enter a file name for the recording and click OK.
Источник