-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[camera_android][0.10.10+2] Pausing the debugger (reaching a breakpoint) after startImageStream() causes memory leak, then OOM crash #166533
Comments
…read paused (flutter#166533) When streaming images using CameraController.startImageStream(), images are being post()-ed to the main looper even if it's halted. This is common when debugging. This quickly results in an OOM and the app crashes abruptly. The fix is done by counting the number of images that have been posted to the main thread but have not yet arrived. If too many images are in transit (I arbitrarily chose 3 as the maximum allowable amount) subsequent frames are dropped, until the main thread receives the pending images. A log message is emitted for each dropped frame. Fixes flutter/flutter#166533 That issue also provides a demo program.
Reproducible using the code sample and steps outlined above. flutter doctor -v
|
@kwikwag I wonder if this isn't due to the fact the debugger does not pause all isolates when a breakpoint is hit, see the issue I created a year and a half ago. Maybe @DanTup has some progress on this, or maybe there is an option somewhere, to be specified? Other packages might be affected by this behavior whereby a background isolate is crashing, not the main one. Unexplainable crashes and random 'Lost connection to device' messages make people very frustated with Flutter, as was again pointed out by a user last week. |
I don't know enough about this issue to know if this is related, but pausing all isolates when a breakpoint is noted in dart-lang/sdk#54978. It's something that would likely be done in the VM and/or DDS (not the VS Code extension or debug adapter), but I don't know if anything has been started on this. |
I have submitted a pull request to solve this at the plugin Android code level. This issue will also arise if the main thread is just lagging - image frames will start piling up and perhaps even form a vicious cycle to kill the app via an OOM error. So it's not just a debugger thing. |
Steps to reproduce
If the debugger is paused any time after
startImageStream()
is called, or if the main thread hangs for long enough for some other reason, there appears to be some kind of buffer that's filling up causing an out-of-memory error to ensue and the app crashes.My use-case: I need to run the images from the camera through a machine-learning model (TFLite/LiteRT). For this purpose I use the
startImageStream()
listener.I cannot use the endorsed CameraX implementation of camera 0.11.1 (camera_android_camerax) due to a show-stopping bug (#152763) that crashes the app spontaneously with a
NullPointerException
when usingstartImageStream()
.This is distinct from #97941, which affected iOS.
Expected results
Using
startImageStream()
should not crash the app, neither spontaneously nor when the debugger is paused or the main thread otherwise busy.Actual results
In camera 0.10.6, or camera 0.11.1 with camera_android 0.10.10+2, when pausing the debugger at some point where the image stream is being listened to (
startImageStream()
was called), the app accumulates memory and eventually crashes due to a platform OOM (out-of-memory) error. Equivalently, the crash will occur if the main thread hangs for long enough.Code sample
Code sample
pubspec.yaml
main.dart
Screenshots or Video
Screenshots / Video demonstration
(The demo uses a slightly different version of the code, which doesn't halt every 100 frames, but instead I entered a debug breakpoint manually)
App with camera plugin 0.10.6 crashes within seconds of reaching the breakpoint. The same thing happens with camera 0.11.1 together with camera_android 0.10.10+2.
issue_flutter_camera-0.10.6-android_memory_leak.webm
Same app with camera plugin version 0.11.1 has no memory leak when reaching breakpoint, but crashes spontaneously after about one minute.
issue_flutter_camera-0.11.1-spontaneous_crash.webm
Logs
Logs
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: