google_ml_kit_flutter
google_ml_kit_flutter copied to clipboard
[Android] - Crash on low spec devices `OutOfMemoryError`
I've been using ML Kit for OCR and barcode reading, it's running fine on devices with higher amount of RAM, but for the lower spec devices for example 2GB of RAM it crashes oom :
here is crashlytics logs:
Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 3686736 byte allocation with 483000 free bytes and 471KB until OOM, target footprint 134217728, growth limit 134217728
at java.util.Arrays.copyOf(Arrays.java:3161)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at io.flutter.plugin.common.StandardMessageCodec.writeBytes(StandardMessageCodec.java:6)
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:198)
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:375)
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:322)
at io.flutter.plugin.common.StandardMessageCodec.writeValue(StandardMessageCodec.java:375)
at io.flutter.plugin.common.StandardMethodCodec.encodeSuccessEnvelope(StandardMethodCodec.java:11)
at io.flutter.plugin.common.EventChannel$IncomingStreamRequestHandler$EventSinkImplementation.success(EventChannel.java:45)
at io.flutter.plugins.camera.Camera.lambda$setImageStreamImageAvailableListener$16(Camera.java)
at io.flutter.plugins.camera.-$$Lambda$Camera$_PNg4o6g6FzKx0u103WoE-p_kVA.run(-.java:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7649)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Using resolution ResolutionPreset.high or above crashes on lower devices.
On my Moto G7 it only works on ResolutionPreset.low and ResolutionPreset.medium
Closing this issue as the memory management should be done to prevent the crash. especially on lower devices if it works for you use lower res, otherwise, try to minimize the memory use by preventing repetitive builds when not necessary.