RecyclerView: Too much inflation / Create taking too long
RecyclerView: Bind taking too long
RecyclerView or ListView: layout / draw taking too long
ListView: Inflation
Layout performance
Layout performance: Cost
Layout performance: Frequency
Rendering performance
Rendering performance: UI Thread
If Record View#draw is taking a long time, it’s often the case that a bitmap is being painted on the UI thread. Painting to a bitmap uses CPU rendering, so should generally should be avoided. 使用Android CPU Profiler 确诊问题
Run a ==release== (or at least non-debuggable) version of your app. The ART runtime disables several important optimizations in order to support debugging features, so make sure you’re looking at something similar to what a user will see.
Enable Profile GPU Rendering. Profile GPU Rendering displays bars on the screen that give you a quick visual representation of how much time it takes to render the frames of a UI window relative to the 16-ms-per-frame benchmark. Each bar has colored components that map to a stage in the rendering pipeline, so you can see which portion is taking the longest. For example, if the frame spends a lot of time handling input, you should look at your app code that handles user input.
There are certain components, such as RecyclerView, that are a common source of jank. If your app uses those components, it’s a good idea to run through those parts of the app.
Sometimes, jank can be reproduced only when the app is launched from a cold start.
Try running your app on a slower device to exacerbate the problem.
Systrace shows when each frame is drawn and color codes each frame to highlight slow render times. This helps you find individual janky frames more accurately than visual inspection. For more information, see Inspecting Frames.
Systrace detects problems in your app and displays alerts both in individual frames and the alerts panel. Following directions in the alert is your best option.
Parts of the Android framework and libraries, such as RecyclerView, contain trace markers. So, the systrace timeline shows when those methods are executed on the UI thread and how long they take to execute.
If systrace doesn’t show you details about why UI thread work is taking for a long time, then you’ll need to use Android CPU Profiler to record either a sampled or instrumented method trace.
If you can’t reproduce jank on a local device, you can build custom performance monitoring into your app to help identify the source of jank on devices in the field.
To fix jank, inspect which frames aren’t completing in 16.7ms, and look for what is going wrong. Is Record View#draw taking abnormally long in some frames, or perhaps Layout? See the Common sources of jank below for these problems, and others.
To avoid jank, long running tasks should be run asynchronously outside of the UI thread. Always be aware of what thread you’re code is running on and use caution when posting non-trivial tasks to the main thread.
If you have a complex and important primary UI for your app (maybe the central scrolling list), consider writing instrumentation tests that can automatically detect slow render times and run the tests frequently to prevent regressions. For more information, see the Automated Performance Testing Codelab.
Automated Test codelab
Topic
How to quickly navigate some Android Performance tools
How the Espresso testing framework can be used to write unit and performance tests
How to use MonkeyRunner and Gradle to automate the testing workflow
How to review systrace output to understand your app’s performance issues