android
runtime_error
ai_generated
true
android.database.CursorWindowAllocationException: 游标窗口分配2048kb失败。打开游标数=10(本进程打开游标数=10)
android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=10 (# cursors opened by this proc=10)
ID: android/room-cursor-window-allocation-failure
92%修复率
88%置信度
1证据数
2023-06-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Room 2.5.0 | active | — | — | — |
| SQLite 3.40.0 | active | — | — | — |
| Android 13 (API 33) | active | — | — | — |
根因分析
打开游标过多且未正确关闭,超出每进程游标限制(通常100个)或内存限制。
English
Too many open cursors without proper closure, exceeding the per-process cursor limit (typically 100) or memory limit.
官方文档
https://developer.android.com/reference/android/database/CursorWindowAllocationException解决方案
-
Replace raw Cursor queries with Room's LiveData or Flow: @Query("SELECT * FROM users") fun getAllUsers(): LiveData<List<User>>. This ensures cursors are closed when lifecycle ends. -
If using raw Cursor, call cursor.close() in finally block and use ContentResolver.query with auto-close flag: val cursor = contentResolver.query(uri, null, null, null, null)?.use { it }
无效尝试
常见但无效的做法:
-
Increasing heap size in AndroidManifest with android:largeHeap=true
90% 失败
Does not address cursor leak; heap size increase delays but doesn't prevent crash.
-
Adding try-finally blocks around cursor usage without using Room's LiveData or Flow
70% 失败
Manual cursor management is error-prone; Room's reactive types handle lifecycle automatically.