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 grasp of Compose's stability system. First, use Layout Inspector to find high-recomposition areas. Then, enable and analyze the Compose compiler report to diagnose unstable parameters (like List) or lambdas.

What's really being asked

This tests your ability to apply a systematic, tool-driven approach to diagnosing non-trivial performance issues in Compose. It probes your understanding of the contract between your data types and the Compose compiler, which is the core of recomposition optimization. The interviewer wants to see that you can move beyond basic state management and debug the underlying stability model.

The full answer

A four-step strategy. First, use the Layout Inspector in Android Studio to visually identify which composables have a high recomposition count. This narrows the search space. Second, enable the Compose compiler metrics by adding the compiler argument in your Gradle file. Third, analyze the generated reports (class_stability.txt, composable.txt) to find unstable parameters or unskippable composables corresponding to the problem areas. Explain that unstable types, like a standard List, prevent skipping because the compiler can't guarantee their contents haven't changed. Fourth, explain lambda stability: a lambda that captures an unstable variable becomes unstable itself. A lambda that is re-allocated on every recomposition (e.g., { viewModel.onItemClick(it) }) is also considered unstable unless wrapped in remember.

The mistakes people make

A major red flag is suggesting solutions without a diagnosis. For example, immediately saying "just wrap everything in remember" without first explaining how you would identify the actual problem. Another weak answer is only mentioning the Layout Inspector's recomposition counter without knowing the next step: using the compiler reports to find the root cause of why it's recomposing. Vaguely mentioning "immutability" without connecting it to specific unstable types like List or Map versus their kotlinx.collections.immutable counterparts is a sign of shallow knowledge.

What usually comes next

How would you handle a class from an external library that you can't modify but is causing instability? (Answer: Create a stable wrapper class for it). When would you use the @Immutable or @Stable annotation, and what are the risks? (Answer: Use it when you can guarantee the contract; the risk is lying to the compiler, leading to subtle UI bugs where the UI doesn't update when it should). How does remember { { ... } } help with lambda stability? (Answer: It ensures the same lambda instance is passed across recompositions, making it stable as long as it doesn't capture unstable inputs).

A concrete example

A ViewModel exposes val items: List<Item>. A parent composable passes this list to a child ItemList(items). The Layout Inspector shows ItemList recomposes every time anything in the parent's state changes, even unrelated state. The compiler report for ItemList shows its items parameter is unstable. The fix is to change the ViewModel to expose val items: ImmutableList<Item> using kotlinx.collections.immutable. This makes the parameter stable, allowing the compiler to mark ItemList as skippable and avoid unnecessary work.

Interview question

You've used the Layout Inspector and found a composable with a high recomposition count. What is the most effective next step to diagnose the root cause?

  • a.Add `Log` statements inside the composable to track which parameters are changing on each recomposition.
  • b.Replace all standard `List` parameters with `ImmutableList` to ensure stability and allow skipping.
  • c.Wrap all parameters passed to the composable with `remember` to prevent them from being re-allocated.
  • d.Enable and analyze the Compose compiler reports to check for unstable parameters or unskippable composables.Correct
Why?

The compiler reports provide a definitive diagnosis of stability issues, which is the root cause of unskippable composables. Applying fixes like using `ImmutableList` or `remember` without this diagnosis is premature.

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