Skip to main content

Detecting a performance issue on the main thread

This tutorial guides you through identifying main thread delays in your apps using the Kotzilla Platform.

info

Issue context

A component resolution is running on main thread for more than 100 ms. ANRs (Application Not Responding) occur when the main thread is blocked for too long, making the app unresponsive and causing it to freeze. Common causes include heavy computations or synchronous operations running on the main thread.

Why it matters?

Main thread blockages severely degrade the user experience, slowing down the responsiveness of your app, which can lead to negative reviews, higher uninstall rates, and reduced app store rankings

Suggested user resolution actions

To address these main-thread blockages, aim to reduce time-consuming components on the main thread, particularly main thread resolution times exceeding 100 ms.

  • Monitor for blocking operations: Use the Kotzilla Platform to detect tasks on the main thread that exceed 100 ms.
  • Background thread offloading: Move blocking tasks like I/O operations or data processing off the main thread using background processing techniques.
  • Refactor code: Simplify or optimize complex methods to minimize main thread impact.
  • Offload heavy tasks: Use Koin to ensure that dependencies handling heavy computations or I/O operations are executed on background threads
  • Optimize dependency configuration: Ensure no heavy tasks are performed during dependency creation in Koin modules.

Prerequisites

In this tutorial, we will be using the NowInAndroid app as an example. Before you begin, ensure you've completed the first two steps of the Get started with Kotzilla Platform

Step 1 - Simulate a blockage on the main thread

To observe the effects of blocking on the main thread, we’ll simulate a 1-second delay in MainActivityViewModel. This delay will serve as an example of a blocking operation impacting the app’s main thread performance.

Here’s the code to introduce a 1-second delay in the MainActivityViewModel:

class MainActivityViewModel(
userDataRepository: UserDataRepository,
) : ViewModel() {

init {
// Simulate a main-thread block with a 1-second delay
Thread.sleep(1000)
}
}

Step 2- Create user sessions to capture app execution data.

With the simulated delay added, create a user session that will allow the Kotzilla SDK to capture and display the issue.

Run the Now In Android app as described in this guide to simulate a repeatable sequence of interactions. Repeat this three times to create consistent data on Kotzilla Platform.

Step 3- Investigate root cause of issues

With the simulated delay in place, the Kotzilla Platform now automatically detects a main thread performance issue on a ViewModel of the app that is taking more than 100ms to resolve. You can quickly view and investigate these issues directly within your IDE using the Koin IDE Plugin.

3.1 View issues in the Koin IDE Plugin

  • Open the Issues Panel in the Koin IDE Plugin within your development environment (Android Studio or IntelliJ)
  • Click on the Application Issues tab

Here, you will see a list of automatically detected issues, including the main thread blockage simulated with the 1-second delay in MainActivityViewModel

3.2 Investigating the root cause of issues

To investigate the details of this issue simply click on the MainActivityViewModel Resolution is Blocking Main Thread issue in the Koin IDE Plugin. This will redirect you to the Issue Details view in the Kotzilla Console.

Here, you will see:

  • A list of 3 impacted user sessions.
  • A detailed description of the issue, including how the resolution of MainActivityViewModel is blocking the main thread.

3.3 User session analysis to trace the main thread blockage

Click on the first impacted user session to drill down into the specific events and components related to the issue. This will take you directly to the Timeline View, where you will observe:

  • The resolution of MainActivityViewModel taking 1022ms
  • The ability to drill down into other potential issues related to its dependency graph

The graphical Timeline View illustrates how the delay affects the screen flow, particularly the transition between the started and resumed states of the main screen. In the Thread section of the Timeline, you can also clearly observe the 1-second delay on the main thread caused by the MainActivityViewModel resolution.

Key Takeaways

By simulating a 1-second delay in MainActivityViewModel, we explored how Kotzilla Platform detects and analyzes main-thread delays that can lead to ANRs. The tutorial demonstrated how main-thread delays affect the session timeline, dependency structure, and app startup time, providing insights that allow you to optimize dependency loading and avoid blocking operations on the main thread.