调试动画卡顿问题
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对于应用开发者来说,最困难的问题之一就是确保动画流畅、无故障且无卡顿。当系统也在执行会占用大量资源的后台任务时,此问题尤其难以调试。没有简单的方法来确定某些卡顿是由您的应用还是系统导致的。不过,我们提供了一个性能分析器工具,可帮助您确定不良行为的可能来源。
在 ChromeOS 上渲染
经过细致优化的应用(如游戏)通常会使用双重缓冲来尽可能缩短用户响应时间。尽管如此,仍有很多因素可能会降低性能。例如,如果渲染某一帧花费的时间太长,渲染的结果还没有为下一次缓冲区交换做好准备,因而前一帧会重复出现。
然后,渲染程序将无法开始渲染下一帧,从而导致更多问题。这种情况对于 Android 移动开发者来说很熟悉;当应用在 ChromeOS 上运行时,上下文会更加复杂。
在桌面设备上运行的应用不会直接渲染到屏幕的显示帧,而是将其数据渲染成纹理。通常有多个应用,每个应用都将其图形渲染成纹理。系统会使用合成器在屏幕上构建视图,将所有纹理组合成一张桌面图片。
合成器在后台以透明方式工作。不过,它引入了一帧时间延迟,以最大限度地利用 GPU 管道。在理想情况下,可能没有必要这样做,但它可以缓解系统性能波动并有助于平衡不对称负载。
当操作系统工作非常吃力时,GPU 可能会遭到挤压。从渲染帧到它显示在屏幕上,可能会有一定延迟。根据硬件,系统可能会使用四重缓冲进行补偿。即使有了更深的缓冲,图形管道仍可能会出现故障。
ARC 图形跟踪器
ChromeOS 具有一个分析工具,可以显示缓冲区如何渗透整个系统、何时发生内存交换、CPU/GPU 的繁忙程度以及应用在给定时间执行的操作,如下图所示:

设置性能分析器
如需使用性能分析器,您必须运行 M75 或更高版本。为获得最佳效果,请使用 Intel 设备。
在使用性能分析器之前,为您的应用植入跟踪记录。将 Trace.traceCounter(Trace.TRACE_TAG_GRAPHICS, "Event", <number>);
添加到代码中您希望包含跟踪记录的任意位置。请使用以前缀 customTrace
开头的 Event
。此前缀未出现在轨迹消息中。
如需设置性能剖析器,请按以下步骤操作:
- 开启开发者模式。
- 开启 Chrome 设置并启用 ARC 图形缓冲区可视化工具。
- 进入
chrome://arc-graphics-tracing
。
运行性能分析器
- 选择卡顿时停止。
- 运行 Android 应用。
- 当 Android 应用处于活动状态并具有焦点时,按 Ctrl+Shift+G。
当发生某种卡顿现象时,系统会弹出一个浏览器窗口。
使用 W 和 S 键可缩放时间轴。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Debug animation jank\n\nOne of the most difficult problems for app developers is\nensuring smooth, glitchless, and jank-free animation. This is especially hard to\ndebug when the system is also performing resource-intensive background\ntasks. There is no easy way to determine whether some jank is caused by your app or\nthe system. However, there is a profiler tool that can help you identify the\npossible source of the bad behavior.\n\nRender on ChromeOS\n------------------\n\nA fine-tuned app, like a game, usually uses double buffering to keep the user\nresponse time as low as possible. Still, there are many things that can degrade\nperformance. For example, if rendering a frame takes too long, the rendered\nresult is not ready for the next buffer swap, and consequently the previous\nframe repeats.\n\nThen, the renderer can't start rendering the next frame,\ncausing even more problems. This scenario is familiar to Android mobile\ndevelopers; when an app runs on ChromeOS, the context is even more complicated.\n\nAn app running on the desktop doesn't render directly to the screen's display\nframe. It renders its data into a texture instead. There are usually multiple\napps, each rendering its graphics into a texture. The system constructs the view\non the screen using a compositor to combine all the textures into a single\ndesktop image.\n\nThe compositor works transparently in the background. However, it introduces a\none-frame time delay to maximize the use of the GPU pipeline. In an ideal\nworld this might not be necessary, but it smooths system performance\nfluctuations and helps balance an asymmetrical load.\n\nWhen the OS is working very hard, the GPU might get squeezed. There can be a\ndelay from the time a frame renders to when it appears on the\nscreen. Depending on the hardware, the system might use quadruple buffering to\ncompensate. Even with deeper buffering, the graphic pipeline can still glitch.\n\nThe ARC graphics tracer\n-----------------------\n\nChromeOS has a profiling tool that shows how the buffers are percolating\nthrough the system, when memory swaps occur, how busy the CPU/GPU is, and what\nyour application is doing at a given time, shown in the following image:\n\n### Set up the profiler\n\nTo use the profiler, you must run M75 or later.\nFor best results, use an Intel device.\n\nBefore using the profiler, seed your app with traces.\nAdd `Trace.traceCounter(Trace.TRACE_TAG_GRAPHICS, \"Event\", \u003cnumber\u003e);` to your code\nwherever you'd like to include a trace. Use an `Event` that begins with\nthe prefix `customTrace`. The prefix doesn't appear in the trace message.\n\nTo set up the profiler, follow these steps:\n\n1. Turn on developer mode.\n2. Turn on Chrome settings and enable the **ARC graphic buffers visualization tool**.\n3. Navigate to `chrome://arc-graphics-tracing`.\n\n### Run the profiler\n\n1. Select **stop on jank**.\n2. Run the Android app.\n3. When the Android app is active and has focus, press \u003ckbd\u003eControl\u003c/kbd\u003e+\u003ckbd\u003eShift\u003c/kbd\u003e+\u003ckbd\u003eG\u003c/kbd\u003e.\n\nWhen some jank happens, a browser window pops up.\nUse the \u003ckbd\u003eW\u003c/kbd\u003e and \u003ckbd\u003eS\u003c/kbd\u003e keys to zoom and shrink the timeline."]]