OutOfMemoryError flutter resource_error ai_generated true

Unhandled exception: OutOfMemoryError: Isolate allocation failed due to memory limit

ID: flutter/isolate-memory-limit

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
1Evidence
2024-01-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Flutter 3.10 active
Dart 3.0 active
Android 12 active
iOS 15 active

Root Cause

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.

generic

中文

Dart Isolate 超出了其内存预算(移动端默认约 100MB),通常是由于在 Isolate 内加载大型数据集或无限递归而未正确释放所致。

Official Documentation

https://api.dart.dev/stable/dart-isolate/Isolate-class.html

Workarounds

  1. 85% success 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))); }`
    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))); }`
  2. 80% success Use streaming isolates with `Isolate.stream` or `ReceivePort` to handle data incrementally, releasing memory after each chunk is processed.
    Use streaming isolates with `Isolate.stream` or `ReceivePort` to handle data incrementally, releasing memory after each chunk is processed.
  3. 75% success Optimize data structures: replace large lists with iterators or lazy generators that don't load all data into memory at once.
    Optimize data structures: replace large lists with iterators or lazy generators that don't load all data into memory at once.

中文步骤

  1. 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))); }`
  2. Use streaming isolates with `Isolate.stream` or `ReceivePort` to handle data incrementally, releasing memory after each chunk is processed.
  3. Optimize data structures: replace large lists with iterators or lazy generators that don't load all data into memory at once.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The `maxMemory` parameter is not supported on mobile platforms; the limit is enforced by the operating system and cannot be increased by the app.

  2. 70% fail

    `Compute` still runs in an isolate with the same memory constraints; passing a large list directly will still cause OOM.

  3. 85% fail

    Assertions do not prevent OOM; they only check logical conditions. Memory allocation failures are not caught by assertions.