# java.lang.StackOverflowError: 栈大小8MB在androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd

- **ID:** `android/compose-recomposition-infinite-loop`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 86%

## 根因

Jetpack Compose中无限重组循环，由于状态变量在组合期间被更新（例如，在没有正确键的LaunchedEffect中调用setState，或触发重新渲染的可组合lambda中）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Compose BOM 2024.01.00 | active | — | — |
| Compose Compiler 1.5.8 | active | — | — |
| Kotlin 1.9.22 | active | — | — |
| Android 14 (API 34) | active | — | — |

## 解决方案

1. ```
   Move the state update outside of composition by using LaunchedEffect with a stable key. Example: LaunchedEffect(Unit) { delay(100); myState.value = newValue } instead of LaunchedEffect(myState.value) { ... }
   ```
2. ```
   Use remember with a derivedStateOf to compute values without triggering recomposition. Example: val derivedValue by remember { derivedStateOf { computeExpensive(myState.value) } }
   ```
3. ```
   Add a snapshotFlow block to observe state changes outside composition. Example: LaunchedEffect(Unit) { snapshotFlow { myState.value }.collect { /* update other state */ } }
   ```

## 无效尝试

- **Increase stack size via AndroidManifest android:largeHeap="true"** — largeHeap increases heap memory, not stack size. Stack size is fixed per thread in Android (8MB default). This does not fix the infinite loop. (95% 失败率)
- **Wrap the state update in withFrameMillis or withFrameNanos** — These APIs schedule work on the next frame but do not prevent the recomposition loop if the state is still updated during composition. (80% 失败率)
