TechBuzzPro

System Tracing in Android: What It Is and How to Use It

· 6 min read · Phones & Tablets

In this article
  1. Background and Purpose of System Tracing
  2. How System Tracing Works
  3. Enabling and Using System Tracing
  4. Common Use Cases and Examples
  5. Common Problems and Limitations
  6. Comparison with Other Profiling Tools
  7. Frequently Asked Questions

System tracing in Android is a diagnostic tool used to record and analyze system-level performance data, such as CPU usage, app responsiveness, and resource contention, to help developers identify performance bottlenecks. It is a standard term used in Android development and is not slang or informal.

Background and Purpose of System Tracing

System tracing originated as part of Android’s ongoing effort to improve app performance and user experience on mobile devices with limited resources. As Android apps became more complex, the need for detailed performance insights grew. System tracing was introduced to give developers a low-overhead way to capture real-time data from the operating system kernel, Android framework, and running apps.

The primary purpose is to diagnose performance issues like jank (stuttering UI), slow app launches, or high battery drain. It allows developers to see how system resources are being used across multiple processes and threads over time. This data is especially useful during app development, optimization, and debugging phases.

System tracing is built into the Android platform and is accessible through developer tools such as Android Studio and the command-line tool adb. It does not require rooting the device and operates within the boundaries of Android’s security model.

How System Tracing Works

System tracing captures data by enabling specific trace points in the Linux kernel and Android runtime. These trace points log events such as function calls, scheduler activity, disk I/O, and graphics rendering. The data is collected into a trace file, typically in .json or .html format, which can be viewed in a browser or Android Studio’s CPU Profiler.

The process works as follows:

  1. A developer initiates a trace session using adb shell systrace or the CPU Profiler in Android Studio.
  2. The Android system enables kernel and userspace trace points for a specified duration (e.g., 10 seconds).
  3. Events are buffered in memory and written to a file when the trace ends.
  4. The trace file is pulled to a computer for analysis.

The trace includes timelines showing CPU usage per core, thread activity, app lifecycle events, and system service calls. For example, a developer might see that a background service is waking the CPU too frequently, causing battery drain.

Enabling and Using System Tracing

To use system tracing, a device must have USB debugging enabled. The exact menu path may vary slightly by device or Android version, but generally:

  1. Go to Settings > About Phone and tap Build Number seven times to enable Developer Options.
  2. Go to Settings > System > Developer Options (or Settings > Additional Settings > Developer Options on some devices).
  3. Enable USB Debugging.

Then, connect the device to a computer with the Android SDK installed. Use one of these methods:

  • Using ADB command: Run adb shell systrace -a com.example.app -o trace.html sched gfx wm am to trace app com.example.app with categories for scheduler, graphics, window manager, and activity manager.
  • Using Android Studio: Open the CPU Profiler, select the target device and app, then click Record using the System Trace template.

After stopping the trace, the file can be opened in Chrome or Edge to view an interactive timeline. Each process and thread is displayed as a horizontal lane, with colored bars indicating activity.

Common Use Cases and Examples

System tracing is commonly used in the following scenarios:

  • Diagnosing UI jank: A developer notices that scrolling in their app is not smooth. The trace shows that the main thread is blocked by a database query, indicating the need to move the query to a background thread.
  • Reducing battery drain: A trace reveals that a background sync job runs every 5 minutes, waking the CPU. The developer adjusts the job to use WorkManager with more efficient constraints.
  • Optimizing app startup: A trace shows that the app spends 800ms inflating a complex layout. The developer simplifies the layout or uses ViewStub to defer loading.
  • Debugging ANRs (Application Not Responding): The trace shows the main thread waiting on a network call, confirming a long-running operation on the UI thread.

These examples show how system tracing provides actionable insights that are not visible through logs or simple performance counters.

Common Problems and Limitations

While powerful, system tracing has limitations:

  • Trace overhead: Although designed to be low-impact, tracing can slightly alter system behavior, especially on low-end devices.
  • Short capture windows: Traces are typically limited to 10–30 seconds to avoid memory issues, making it hard to capture rare or long-tail issues.
  • Complex output: The trace file contains a large amount of data. New users may find it difficult to interpret without training.
  • Device compatibility: Some older Android versions or custom ROMs may have incomplete trace support.

Additionally, system tracing cannot capture app-specific code-level details (like function execution time within an app) unless combined with method tracing or debug symbols. For that, developers may need to use the Debug.startMethodTracing() API or sample-based profiling.

Comparison with Other Profiling Tools

System tracing is one of several performance tools available to Android developers:

  • Method Tracing: Captures call stacks and execution time per method. More detailed than system tracing but has higher overhead and is not suitable for real-time UI analysis.
  • CPU Profiler (in Android Studio): Combines system tracing, method tracing, and memory data into a unified interface. System tracing is a mode within this tool.
  • Perfetto: A next-generation tracing platform that supersedes systrace. It supports longer traces, more data sources, and better querying. Google recommends Perfetto for new projects.
  • Logcat: Provides textual logs but lacks timing and concurrency context. Useful for errors but not for performance analysis.

System tracing is best for understanding system-wide interactions, while method tracing or Perfetto may be better for deep app-specific analysis.

Frequently Asked Questions

Is system tracing available on all Android devices?

Yes, system tracing is supported on all Android devices running Android 4.3 (Jelly Bean MR2) or later, provided USB debugging is enabled. However, some trace categories may not be available on all devices or custom ROMs.

Do I need root access to use system tracing?

No, root access is not required. System tracing works through the standard Android debugging interface (ADB) and operates within the permissions granted to developer mode.

Can system tracing slow down my device?

Tracing has minimal overhead, but it can slightly affect performance during capture, especially on older or low-RAM devices. The impact is usually negligible for short traces (under 30 seconds).

What is the difference between systrace and Perfetto?

Systrace is the older command-line tool for system tracing. Perfetto is its modern replacement, offering more features, longer trace durations, and better data querying. Google now recommends Perfetto for new development.

Can I use system tracing on a production app?

System tracing is intended for development and testing. It requires developer access and ADB, so it cannot be used by end users. However, the insights gained can be applied to improve production apps.

How do I analyze a system trace file?

Open the trace file (usually .html) in Chrome or Edge. Use the zoom, search, and selection tools to inspect timelines, identify long-running tasks, and check CPU usage per core and thread.