未处理的异常:OutOfMemoryError:由于内存限制,Isolate 分配失败
Unhandled exception: OutOfMemoryError: Isolate allocation failed due to memory limit
ID: flutter/isolate-memory-limit
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Flutter 3.10 | active | — | — | — |
| Dart 3.0 | active | — | — | — |
| Android 12 | active | — | — | — |
| iOS 15 | active | — | — | — |
根因分析
Dart Isolate 超出了其内存预算(移动端默认约 100MB),通常是由于在 Isolate 内加载大型数据集或无限递归而未正确释放所致。
English
A Dart isolate exceeded its memory budget (default ~100MB on mobile), often caused by loading large datasets or infinite recursion within the isolate without proper disposal.
官方文档
https://api.dart.dev/stable/dart-isolate/Isolate-class.html解决方案
-
Process data in chunks: split the large dataset into smaller batches and send each batch to the isolate using `SendPort.send`. Example: `for (var i = 0; i < data.length; i += chunkSize) { isolateSendPort.send(data.sublist(i, min(i+chunkSize, data.length))); }` -
Use streaming isolates with `Isolate.stream` or `ReceivePort` to handle data incrementally, releasing memory after each chunk is processed.
-
Optimize data structures: replace large lists with iterators or lazy generators that don't load all data into memory at once.
无效尝试
常见但无效的做法:
-
90% 失败
The `maxMemory` parameter is not supported on mobile platforms; the limit is enforced by the operating system and cannot be increased by the app.
-
70% 失败
`Compute` still runs in an isolate with the same memory constraints; passing a large list directly will still cause OOM.
-
85% 失败
Assertions do not prevent OOM; they only check logical conditions. Memory allocation failures are not caught by assertions.