Skip to content
tezvyn:

How do you diagnose and fix excessive recomposition in Jetpack Compose?

Source: developer.android.comHardHow cards are made

How do you diagnose and fix excessive recomposition in Jetpack Compose?

This tests your understanding of Compose's stability system for performance. Use the Layout Inspector and Compiler Metrics to find unstable composables, then fix them with immutable data types and stable lambdas.

What's really being asked

This question tests your senior-level ability to diagnose and solve non-obvious performance problems in Jetpack Compose. It's not about basic state management, but about the core compiler optimization mechanism: stability. The interviewer wants to see if you can connect a symptom (UI jank, high recomposition) to its root cause (unstable inputs preventing recomposition skipping) and use the right tools to prove it.

The full answer

A strong answer outlines a systematic, three-step process. First, identify the problem area using the Layout Inspector in Android Studio to find composables with unexpectedly high recomposition counts. Second, diagnose the root cause by enabling Compose Compiler Metrics. This generates a report detailing which composables are skippable and restartable, and more importantly, which parameters are marked as stable or unstable. Third, solve the stability issues. This involves replacing standard collections like List with kotlinx.collections.immutable.ImmutableList, annotating custom data classes with @Immutable where appropriate, and refactoring lambdas to avoid capturing unstable variables.

The mistakes people make

A major red flag is stopping at the first step. Many candidates will mention the Layout Inspector and recomposition counts but can't explain why a composable isn't being skipped. They might guess at solutions like remember or derivedStateOf without a clear diagnosis. Another weak answer is describing stability incorrectly, for example, by claiming any val is stable. A val property of type List<String> is unstable because the list's contents can change without the object reference changing.

What usually comes next

Expect questions like: "When would you use the @Stable annotation instead of @Immutable?" (Answer: For classes that are mutable but notify Compose of changes, like a state holder using MutableState). Or, "How can a lambda function be unstable, and how do you fix it?" (Answer: If it captures an unstable variable. Fix by passing the data as a stable parameter to the composable instead of capturing it from the outer scope). Another is "What if the unstable type comes from an external library you can't modify?" (Answer: Create a stable wrapper class that holds the unstable type and exposes its data as stable properties).

A concrete example

Imagine a ContactList composable taking a List<Contact> as a parameter. The compiler metrics report this List as unstable. Even if only one contact's name changes in the parent ViewModel, the entire ContactList recomposes because Compose can't guarantee the list is the same. The fix is to change the parameter type to ImmutableList<Contact>. Now, if the list itself hasn't changed reference, Compose knows it can safely skip recomposing the ContactList. If a contact is updated, the ViewModel must create a new ImmutableList instance, which correctly and efficiently triggers recomposition.

Interview question

When diagnosing excessive recomposition in Compose, which tool is most crucial for identifying why a composable's parameters are preventing skipping?

  • a.Analyzing the ViewModel's state for mutable var properties.
  • b.The Android Studio Layout Inspector's recomposition count.
  • c.The Compose Compiler Metrics report detailing parameter stability.Correct
  • d.Wrapping all composable parameters with remember or derivedStateOf.
Why?

The Compose Compiler Metrics report explicitly details which parameters are marked as stable or unstable, directly explaining why a composable might not be skippable. While the Layout Inspector shows that a composable is recomposing excessively, it does not reveal the underlying reason for its instability.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles