If your app relies heavily on web content and you want optimal performance, you
need to understand how WebView behaves under the hood. Most apps don't need
this level of detail, but for performance-focused apps, you can use
Perfetto, the system-wide performance tracing tool for Android,
to gather detailed performance data. This applies not only to WebView objects
themselves, but to any API your app calls in the android.webkit and
androidx.webkit packages.
Tracing WebView with Perfetto helps you diagnose performance bottlenecks, such as slow startup times, jank, network latency, and the impact of specific WebView APIs on your app's performance.
This document describes how to do the following:
- Record a trace using the Perfetto UI
- Enable the recommended trace categories
- Analyze WebView startup
- Understand the trace
Record a trace using the Perfetto UI
The most straightforward way to record and analyze a WebView trace is by using the Perfetto UI.
Prerequisites
Before you record a trace, make sure you have the following:
- An Android device connected to your workstation with USB debugging enabled.
- ADB installed and working on your workstation.
- A Chromium-based browser on your workstation.
Record the trace
To record a trace, do the following:
- In a Chromium-based browser on your workstation, open the Perfetto UI.
- In the left navigation menu, click Record new trace.
- Configure the trace categories to capture WebView data. See the Recommended trace categories section for the specific toggles to enable.
- Click Start tracing. Don't rely on a fixed trace duration; instead, control the recording manually so that you capture the complete scenario.
- While the trace is recording, interact with your app on the device to reproduce the performance issue.
- When you have reproduced the issue, click Stop. The trace automatically loads in the Perfetto UI timeline for analysis.
Recommended trace categories
To capture meaningful data about WebView performance, you must enable specific trace categories in the Perfetto recording settings.
The categories in this section are a basic set that covers most WebView investigations. Perfetto offers many more probes, such as CPU scheduling, memory, and power, that can be helpful depending on what you're debugging. Take a look at the full list of options in the Perfetto UI and enable any that are relevant to your issue.
Under Probes in the Perfetto UI, configure the following:
Under Android apps & svcs, enable Atrace userspace annotations and select the following categories:
- Activity Manager (
am) - AIDL calls (
aidl) - System Server (
ss) - View System (
view) - WebView (
webview) - Window Manager (
wm)
The
aidl,view, andwmcategories help you correlate WebView events with cross-process calls, view layout and drawing, and window changes.- Activity Manager (
In the same probe, turn on Record events from all Android apps and services. This captures custom trace events emitted by your app and by the system services that WebView interacts with.
Under Chrome browser, enable Chrome browser tracing.
Under Additional Categories, select the following categories:
android_webview.timeline, android_webview, base, blink, benchmark, blink.worker, blink.user_timing, blink.resource, blink.net, blink.console, blink.bindings, browser, Java, startup, navigation, interactions, loading, net, netlog, v8.Under High Overhead Categories, select
disabled-by-default-histogram_samples.
Analyze WebView startup
A common use case for tracing is analyzing WebView startup latency. To ensure you capture the complete initialization process, follow these tips:
Force stop the app before recording: If the app is already running or in the background, you won't capture the cold start performance of WebView. Force stop the app process using ADB:
adb shell am force-stop YOUR_PACKAGE_NAME
Start the trace first: Make sure you click Start tracing in the Perfetto UI before launching your app on the device.
Understand the trace
After your trace loads in the Perfetto UI, you can search for specific events and inspect the timeline. Because internal trace markers can change between Android releases, focus your analysis on the higher-level categories you enabled:
android_webviewandandroid_webview.timeline: These events show the lifecycle of WebView initialization and rendering.blinkandv8: Look for bottlenecks in HTML parsing, layout calculation, and JavaScript execution.netandnetlog: Examine these events for network requests, response latencies, and resource loading delays.navigationandstartup: These events correspond to page loading phases and the initial setup of the browser engine.
Additional resources
For general guidance on capturing system traces on Android, navigating the Perfetto UI, and interpreting non-WebView events, see the following resources:
- Overview of system tracing: An introduction to the system tracing tools available on Android, including Perfetto.
- App startup analysis and optimization: How to use Perfetto traces to analyze and improve app startup.
- Tracing 101: An overview of tracing concepts from the Perfetto documentation.
- Perfetto UI: How to navigate the timeline, search for events, and query a trace.
- Android trace analysis: A walkthrough of analyzing common Android performance problems with Perfetto.