Skip to content
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

StartImageStream causes memory leak, frequently crashes [camera_android_camerax] #145893

Closed
jamesstevensdev opened this issue Mar 28, 2024 · 14 comments · Fixed by flutter/packages#6493
Assignees
Labels
c: fatal crash Crashes that terminate the process found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: camera The camera plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. perf: memory Performance issues related to memory platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team

Comments

@jamesstevensdev
Copy link

Steps to reproduce

pubspec.yaml

camera: ^0.10.5+9
camera_android_camerax: ^0.6.1

  1. Paste the attached code anywhere in Flutter application
  2. Add necessary dependencies/permissions to respective Android files
  3. Run application (tested in debug, profile, and prod)

Expected results

Flutter should stream images and clean up the memory periodically.

Actual results

Memory continues to build up until the application crashes. Higher camera resolutions cause crashes much quicker. Occasionally, the memory profiler will show a drastic drop in memory usage before building up again.

Code sample

Code sample
    // Assign the format group based on the platform
    final formatGroup = Platform.isIOS ? ImageFormatGroup.bgra8888 : ImageFormatGroup.yuv420;

    // Set the target camera resolution
    ResolutionPreset resolutionPreset = ResolutionPreset.ultraHigh;

    // Get the correct camera lens
    final cameras = await availableCameras();
    final description = cameras.firstWhere(
      (c) => c.lensDirection == CameraLensDirection.back,
    );

    // Create a camera controller
    final CameraController controller = CameraController(
      description,
      resolutionPreset,
      enableAudio: false,
      imageFormatGroup: formatGroup,
    );

    // Initialize the controller
    await controller.initialize();

    // Start the image stream with an empty callback
    await controller.startImageStream((image) {
      return;
    });

    // Wait for 60 seconds
    await Future.delayed(const Duration(seconds: 60));

    // Stop and dispose the camera controller
    await controller.stopImageStream();
    await controller.dispose();

Screenshots or Video

Screenshots / Video demonstration

Screenshot 2024-03-28 at 6 35 07 AM

Logs

Logs
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
4
I/CameraManagerGlobal(26626): postSingleUpdate device: camera id 0 status STATUS_PRESENT
I/CameraManagerGlobal(26626): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client com.converus.mobile API Level 2User Id 0
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579 INACTIVE
2
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use cases [androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953, androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579] now ATTACHED
D/UseCaseAttachState(26626): All use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953, androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579] for camera: 0
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953] for camera: 0
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=270, isOppositeFacing=false, result=270
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Resetting Capture Session
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Releasing session in state CLOSING
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: CLOSING --> REOPENING
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         OPENING               
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 1 (Max allowed: 1)
D/CameraStateMachine(26626): New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/CameraStateMachine(26626): Publishing new public camera state CameraState{type=OPENING, error=null}
2
E/ObserverFlutterApi(26626): The Observer that received a callback has been garbage collected. Please create a new instance to receive any further data changes.
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579 INACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953] for camera: 0
D/DeferrableSurface(26626): use count-1,  useCount=0 closed=true androidx.camera.core.processing.SurfaceEdge$SettableSurface@3788de6
D/DeferrableSurface(26626): Surface no longer in use[total_surfaces=8, used_surfaces=4](androidx.camera.core.processing.SurfaceEdge$SettableSurface@3788de6}
D/DeferrableSurface(26626): Surface terminated[total_surfaces=7, used_surfaces=4](androidx.camera.core.processing.SurfaceEdge$SettableSurface@3788de6}
D/DeferrableSurface(26626): use count-1,  useCount=0 closed=false androidx.camera.core.SurfaceRequest$2@72fa272
D/DeferrableSurface(26626): Surface no longer in use[total_surfaces=7, used_surfaces=3](androidx.camera.core.SurfaceRequest$2@72fa272}
D/DeferrableSurface(26626): surface closed,  useCount=0 closed=true androidx.camera.core.SurfaceRequest$2@72fa272
D/DeferrableSurface(26626): Surface terminated[total_surfaces=6, used_surfaces=3](androidx.camera.core.SurfaceRequest$2@72fa272}
D/DeferrableSurface(26626): use count-1,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@9665c1f
D/DeferrableSurface(26626): Surface no longer in use[total_surfaces=6, used_surfaces=2](androidx.camera.core.impl.ImmediateSurface@9665c1f}
D/DeferrableSurface(26626): Surface terminated[total_surfaces=5, used_surfaces=2](androidx.camera.core.impl.ImmediateSurface@9665c1f}
D/DeferrableSurface(26626): use count-1,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@be08535
D/DeferrableSurface(26626): Surface no longer in use[total_surfaces=5, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@be08535}
D/DeferrableSurface(26626): Surface terminated[total_surfaces=4, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@be08535}
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} CameraDevice.onClosed()
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Attempting to open the camera.
D/CameraStateRegistry(26626): tryOpenCamera(Camera@922bc09[id=0]) [Available Cameras: 0, Already Open: true (Previous state: OPENING)] --> SUCCESS
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         OPENING               
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 1 (Max allowed: 1)
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Opening camera.
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: REOPENING --> OPENING
D/CameraStateMachine(26626): New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/UseCaseAttachState(26626): All use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953, androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579] for camera: 0
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=270, isOppositeFacing=false, result=270
I/BpBinder(26626): onLastStrongRef automatically unlinking death recipients: <uncached descriptor>
D/DeferrableSurface(26626): surface closed,  useCount=0 closed=true androidx.camera.core.processing.SurfaceEdge$SettableSurface@9bda8a8
D/DeferrableSurface(26626): Surface terminated[total_surfaces=3, used_surfaces=1](androidx.camera.core.processing.SurfaceEdge$SettableSurface@9bda8a8}
D/DeferrableSurface(26626): use count-1,  useCount=0 closed=false androidx.camera.core.SurfaceRequest$2@698a754
D/DeferrableSurface(26626): Surface no longer in use[total_surfaces=3, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@698a754}
D/DeferrableSurface(26626): surface closed,  useCount=0 closed=true androidx.camera.core.SurfaceRequest$2@698a754
D/DeferrableSurface(26626): Surface terminated[total_surfaces=2, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@698a754}
D/ImageCapture(26626): clearPipeline
D/DeferrableSurface(26626): surface closed,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@6ee15f9
D/DeferrableSurface(26626): Surface terminated[total_surfaces=1, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface@6ee15f9}
D/DeferrableSurface(26626): surface closed,  useCount=0 closed=true androidx.camera.core.impl.ImmediateSurface@e4ce39f
D/DeferrableSurface(26626): Surface terminated[total_surfaces=0, used_surfaces=0](androidx.camera.core.impl.ImmediateSurface@e4ce39f}
4
I/CameraManagerGlobal(26626): postSingleUpdate device: camera id 0 status STATUS_NOT_AVAILABLE
I/CameraManagerGlobal(26626): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_OPEN for client com.converus.mobile API Level 2User Id 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953, androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use cases [androidx.camera.core.Preview-bcdbf3d8-99c8-44e3-b09d-85c93c432aa099292953, androidx.camera.core.ImageCapture-caef0ff3-d70c-4426-bb28-d81ffe2b067133284237, androidx.camera.core.ImageAnalysis-fc2861ba-12b9-4ff3-ba67-066c2ce6c61d209322579] now DETACHED for camera
D/UseCaseAttachState(26626): All use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Resetting Capture Session
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Releasing session in state OPENING
2
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Closing camera.
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: OPENING --> CLOSING
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         CLOSING               
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 1 (Max allowed: 1)
D/CameraStateMachine(26626): New public camera state CameraState{type=CLOSING, error=null} from CLOSING and null
D/CameraStateMachine(26626): Publishing new public camera state CameraState{type=CLOSING, error=null}
2
E/ObserverFlutterApi(26626): The Observer that received a callback has been garbage collected. Please create a new instance to receive any further data changes.
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} CameraDevice.onOpened()
5
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/DynamicRangeResolver(26626): Resolved dynamic range for use case androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e to no compatible HDR dynamic ranges.
D/DynamicRangeResolver(26626): DynamicRange@d4d62ea{encoding=UNSPECIFIED, bitDepth=0}
D/DynamicRangeResolver(26626): ->
D/DynamicRangeResolver(26626): DynamicRange@ce67ad5{encoding=SDR, bitDepth=8}
4
I/CameraManagerGlobal(26626): postSingleUpdate device: camera id 0 status STATUS_PRESENT
I/CameraManagerGlobal(26626): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client com.converus.mobile API Level 2User Id 0
2
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} CameraDevice.onClosed()
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: CLOSING --> INITIALIZED
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         CLOSED                
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 0 (Max allowed: 1)
D/CameraStateMachine(26626): New public camera state CameraState{type=CLOSED, error=null} from CLOSED and null
D/CameraStateMachine(26626): Publishing new public camera state CameraState{type=CLOSED, error=null}
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/DeferrableSurface(26626): Surface created[total_surfaces=1, used_surfaces=0](androidx.camera.core.processing.SurfaceEdge$SettableSurface@bb8e074}
D/DeferrableSurface(26626): Surface created[total_surfaces=2, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@fbd8fe0}
D/DeferrableSurface(26626): New surface in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@fbd8fe0}
D/DeferrableSurface(26626): use count+1, useCount=1 androidx.camera.core.SurfaceRequest$2@fbd8fe0
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/ImageCapture(26626): createPipeline(cameraId: 0, streamSpec: StreamSpec{resolution=3840x2160, dynamicRange=DynamicRange@ce67ad5{encoding=SDR, bitDepth=8}, expectedFrameRateRange=[0, 0], implementationOptions=androidx.camera.camera2.impl.Camera2ImplConfig@b9bca0c})
D/DeferrableSurface(26626): Surface created[total_surfaces=3, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@a2cdf55}
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
2
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/DeferrableSurface(26626): Surface created[total_surfaces=4, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@c66d25b}
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806 ACTIVE
2
E/ObserverFlutterApi(26626): The Observer that received a callback has been garbage collected. Please create a new instance to receive any further data changes.
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772 INACTIVE
2
D/UseCaseAttachState(26626): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use cases [androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] now ATTACHED
D/UseCaseAttachState(26626): All use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] for camera: 0
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Resetting Capture Session
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Releasing session in state INITIALIZED
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Attempting to force open the camera.
D/CameraStateRegistry(26626): tryOpenCamera(Camera@922bc09[id=0]) [Available Cameras: 1, Already Open: false (Previous state: CLOSED)] --> SUCCESS
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         OPENING               
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 1 (Max allowed: 1)
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Opening camera.
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: INITIALIZED --> OPENING
D/CameraStateMachine(26626): New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/CameraStateMachine(26626): Publishing new public camera state CameraState{type=OPENING, error=null}
D/UseCaseAttachState(26626): All use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] for camera: 0
D/CameraOrientationUtil(26626): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
4 
I/CameraManagerGlobal(26626): postSingleUpdate device: camera id 0 status STATUS_NOT_AVAILABLE
I/CameraManagerGlobal(26626): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_OPEN for client com.converus.mobile API Level 2User Id 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772 INACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Use case androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772 ACTIVE
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] for camera: 0
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} CameraDevice.onOpened()
D/Camera2CameraImpl(26626): {Camera@922bc09[id=0]} Transitioning camera internal state: OPENING --> OPENED
D/CameraStateRegistry(26626): Recalculating open cameras:
D/CameraStateRegistry(26626): Camera                                       State                 
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Camera@6ecc0e6[id=1]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@922bc09[id=0]                         OPEN                  
D/CameraStateRegistry(26626): Camera@575b979[id=2]                         UNKNOWN               
D/CameraStateRegistry(26626): Camera@2a4a23b[id=3]                         UNKNOWN               
D/CameraStateRegistry(26626): -------------------------------------------------------------------
D/CameraStateRegistry(26626): Open count: 1 (Max allowed: 1)
D/CameraStateMachine(26626): New public camera state CameraState{type=OPEN, error=null} from OPEN and null
D/CameraStateMachine(26626): Publishing new public camera state CameraState{type=OPEN, error=null}
D/UseCaseAttachState(26626): All use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] for camera: 0
D/UseCaseAttachState(26626): Active and attached use case: [androidx.camera.core.ImageCapture-2053cd2b-6ee4-43ed-a08f-cb06c1392191190190806, androidx.camera.core.Preview-85d3634a-7e78-44cb-b2f7-c7f2c404148e38959739, androidx.camera.core.ImageAnalysis-b7f526b1-5214-4b8c-9c4a-826970b66783236936772] for camera: 0
D/SyncCaptureSessionBase(26626): [androidx.camera.camera2.internal.SynchronizedCaptureSessionBaseImpl@4589f17] getSurface...done
D/CaptureSession(26626): Opening capture session.
D/DeferrableSurface(26626): New surface in use[total_surfaces=4, used_surfaces=2](androidx.camera.core.processing.SurfaceEdge$SettableSurface@bb8e074}
D/DeferrableSurface(26626): use count+1, useCount=1 androidx.camera.core.processing.SurfaceEdge$SettableSurface@bb8e074
D/DeferrableSurface(26626): New surface in use[total_surfaces=4, used_surfaces=3](androidx.camera.core.impl.ImmediateSurface@a2cdf55}
D/DeferrableSurface(26626): use count+1, useCount=1 androidx.camera.core.impl.ImmediateSurface@a2cdf55
D/DeferrableSurface(26626): New surface in use[total_surfaces=4, used_surfaces=4](androidx.camera.core.impl.ImmediateSurface@c66d25b}
D/DeferrableSurface(26626): use count+1, useCount=1 androidx.camera.core.impl.ImmediateSurface@c66d25b
D/CaptureSession(26626): Attempting to send capture request onConfigured
D/CaptureSession(26626): Issuing request for session.
D/Camera2CaptureRequestBuilder(26626): createCaptureRequest
I/CameraManagerGlobal(26626): Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_ACTIVE for client com.converus.mobile API Level 2User Id 0
D/CaptureSession(26626): CameraCaptureSession.onConfigured() mState=OPENED
D/CaptureSession(26626): CameraCaptureSession.onReady() OPENED
I/converus.mobile(26626): Background young concurrent copying GC freed 590(114KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 47MB/47MB, paused 8.716ms,28us total 20.731ms
I/converus.mobile(26626): Background young concurrent copying GC freed 283(115KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 107MB/107MB, paused 5.809ms,28us total 33.923ms
I/converus.mobile(26626): Background young concurrent copying GC freed 161(63KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 71MB/71MB, paused 5.424ms,21us total 32.499ms
I/converus.mobile(26626): Background concurrent copying GC freed 824(123KB) AllocSpace objects, 9(63MB) LOS objects, 35% free, 43MB/67MB, paused 5.529ms,96us total 51.553ms
I/converus.mobile(26626): Background young concurrent copying GC freed 592(85KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 103MB/103MB, paused 2.749ms,5.733ms total 29.452ms
W/converus.mobile(26626): Suspending all threads took: 8.048ms
I/converus.mobile(26626): Background young concurrent copying GC freed 603(115KB) AllocSpace objects, 2(11MB) LOS objects, 0% free, 91MB/91MB, paused 3.295ms,8.198ms total 28.013ms
I/converus.mobile(26626): Background concurrent copying GC freed 139(23KB) AllocSpace objects, 6(51MB) LOS objects, 42% free, 32MB/56MB, paused 6.626ms,33us total 35.488ms
I/converus.mobile(26626): Background young concurrent copying GC freed 476(102KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 67MB/67MB, paused 5.483ms,28us total 25.179ms
I/converus.mobile(26626): Background concurrent copying GC freed 280(59KB) AllocSpace objects, 17(102MB) LOS objects, 35% free, 43MB/67MB, paused 7.499ms,57us total 52.006ms
I/converus.mobile(26626): Background concurrent copying GC freed 842(143KB) AllocSpace objects, 17(118MB) LOS objects, 14% free, 142MB/166MB, paused 67us,113us total 106.177ms
I/converus.mobile(26626): Background concurrent copying GC freed 890(137KB) AllocSpace objects, 18(126MB) LOS objects, 35% free, 43MB/67MB, paused 26.758ms,23us total 77.662ms
I/converus.mobile(26626): Background concurrent copying GC freed 880(123KB) AllocSpace objects, 13(63MB) LOS objects, 22% free, 83MB/107MB, paused 5.233ms,25us total 68.956ms
I/converus.mobile(26626): Background young concurrent copying GC freed 641(131KB) AllocSpace objects, 10(55MB) LOS objects, 0% free, 107MB/107MB, paused 10.431ms,26us total 58.220ms
I/converus.mobile(26626): Background young concurrent copying GC freed 620(118KB) AllocSpace objects, 5(27MB) LOS objects, 0% free, 103MB/103MB, paused 25.658ms,79us total 65.939ms
I/converus.mobile(26626): Background concurrent copying GC freed 805(128KB) AllocSpace objects, 14(102MB) LOS objects, 17% free, 115MB/139MB, paused 67us,4.802ms total 122.726ms
I/converus.mobile(26626): Waiting for a blocking GC Alloc
I/converus.mobile(26626): Background concurrent copying GC freed 794(128KB) AllocSpace objects, 17(118MB) LOS objects, 15% free, 131MB/155MB, paused 295us,27us total 139.449ms
I/converus.mobile(26626): WaitForGcToComplete blocked Alloc on Background for 41.780ms
I/converus.mobile(26626): Starting a blocking GC Alloc
I/converus.mobile(26626): Background concurrent copying GC freed 639(118KB) AllocSpace objects, 17(118MB) LOS objects, 16% free, 123MB/147MB, paused 56us,1.088ms total 117.397ms
I/converus.mobile(26626): Waiting for a blocking GC Alloc
I/converus.mobile(26626): Background concurrent copying GC freed 996(148KB) AllocSpace objects, 23(150MB) LOS objects, 18% free, 103MB/127MB, paused 122us,35us total 103.019ms
I/converus.mobile(26626): WaitForGcToComplete blocked Alloc on Background for 18.782ms
I/converus.mobile(26626): Starting a blocking GC Alloc
I/converus.mobile(26626): Background young concurrent copying GC freed 1016(172KB) AllocSpace objects, 12(79MB) LOS objects, 9% free, 115MB/127MB, paused 5.781ms,67us total 45.389ms
I/converus.mobile(26626): Background concurrent copying GC freed 888(149KB) AllocSpace objects, 22(134MB) LOS objects, 22% free, 83MB/107MB, paused 432us,59us total 100.560ms
I/converus.mobile(26626): Background young concurrent copying GC freed 1196(165KB) AllocSpace objects, 13(87MB) LOS objects, 24% free, 75MB/99MB, paused 5.135ms,22us total 41.955ms
I/converus.mobile(26626): Background young concurrent copying GC freed 661(108KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 91MB/91MB, paused 6.034ms,23us total 30.155ms
I/converus.mobile(26626): Background concurrent copying GC freed 888(141KB) AllocSpace objects, 25(162MB) LOS objects, 20% free, 91MB/115MB, paused 7.253ms,80us total 111.805ms
I/converus.mobile(26626): Background young concurrent copying GC freed 569(113KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 51MB/51MB, paused 5.839ms,33us total 22.579ms
I/converus.mobile(26626): Background young concurrent copying GC freed 629(124KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 71MB/71MB, paused 5.687ms,30us total 28.258ms
I/converus.mobile(26626): Background concurrent copying GC freed 771(124KB) AllocSpace objects, 19(130MB) LOS objects, 30% free, 55MB/79MB, paused 90us,28us total 117.873ms
I/converus.mobile(26626): Background concurrent copying GC freed 1070(145KB) AllocSpace objects, 13(71MB) LOS objects, 22% free, 83MB/107MB, paused 12.661ms,23us total 59.285ms
W/converus.mobile(26626): Suspending all threads took: 5.613ms
I/converus.mobile(26626): Background young concurrent copying GC freed 528(120KB) AllocSpace objects, 5(27MB) LOS objects, 0% free, 99MB/99MB, paused 3.580ms,5.766ms total 33.748ms
I/converus.mobile(26626): Background young concurrent copying GC freed 589(115KB) AllocSpace objects, 6(31MB) LOS objects, 0% free, 123MB/123MB, paused 7.275ms,21us total 38.286ms
I/converus.mobile(26626): Background concurrent copying GC freed 815(143KB) AllocSpace objects, 24(174MB) LOS objects, 35% free, 44MB/68MB, paused 7.731ms,21us total 59.687ms
I/converus.mobile(26626): Background concurrent copying GC freed 1382(179KB) AllocSpace objects, 19(114MB) LOS objects, 15% free, 127MB/151MB, paused 6.922ms,70us total 140.113ms
I/converus.mobile(26626): Background young concurrent copying GC freed 480(132KB) AllocSpace objects, 18(110MB) LOS objects, 28% free, 59MB/83MB, paused 7.308ms,23us total 36.951ms
I/converus.mobile(26626): Background young concurrent copying GC freed 781(148KB) AllocSpace objects, 6(31MB) LOS objects, 0% free, 123MB/123MB, paused 5.944ms,28us total 38.983ms
I/converus.mobile(26626): Background young concurrent copying GC freed 164(85KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 43MB/43MB, paused 5.370ms,24us total 20.895ms
I/converus.mobile(26626): Background young concurrent copying GC freed 268(120KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 91MB/91MB, paused 6.931ms,1.699ms total 50.539ms
I/converus.mobile(26626): Background young concurrent copying GC freed 1199(186KB) AllocSpace objects, 12(79MB) LOS objects, 8% free, 83MB/91MB, paused 9.669ms,77us total 78.542ms
I/converus.mobile(26626): Background concurrent copying GC freed 1088(158KB) AllocSpace objects, 17(106MB) LOS objects, 30% free, 55MB/79MB, paused 1.417ms,26us total 130.908ms
D/InputMethodManager(26626): startInputInner - Id : 0
I/InputMethodManager(26626): startInputInner - mService.startInputOrWindowGainedFocus
I/converus.mobile(26626): Background concurrent copying GC freed 455(120KB) AllocSpace objects, 17(118MB) LOS objects, 22% free, 83MB/107MB, paused 59us,1.348ms total 130.715ms
D/InputTransport(26626): Input channel destroyed: 'ClientS', fd=143
D/InputMethodManager(26626): startInputInner - Id : 0
I/InputMethodManager(26626): startInputInner - mService.startInputOrWindowGainedFocus
I/converus.mobile(26626): Background young concurrent copying GC freed 1343(173KB) AllocSpace objects, 13(87MB) LOS objects, 0% free, 154MB/154MB, paused 3.658ms,74us total 132.097ms
I/converus.mobile(26626): Background young concurrent copying GC freed 544(127KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 59MB/59MB, paused 19.792ms,34us total 58.255ms
I/converus.mobile(26626): Background concurrent copying GC freed 762(140KB) AllocSpace objects, 11(71MB) LOS objects, 22% free, 83MB/107MB, paused 786us,2.132ms total 139.564ms
I/converus.mobile(26626): Background young concurrent copying GC freed 867(121KB) AllocSpace objects, 13(87MB) LOS objects, 14% free, 91MB/107MB, paused 108.460ms,23us total 229.041ms
I/converus.mobile(26626): Background young concurrent copying GC freed 585(130KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 87MB/87MB, paused 14.911ms,1.274ms total 37.501ms
I/converus.mobile(26626): Background young concurrent copying GC freed 1114(159KB) AllocSpace objects, 13(87MB) LOS objects, 0% free, 154MB/154MB, paused 12.549ms,80us total 225.039ms
I/converus.mobile(26626): Background concurrent copying GC freed 1672(188KB) AllocSpace objects, 24(154MB) LOS objects, 23% free, 79MB/103MB, paused 41us,90us total 136.457ms
I/converus.mobile(26626): Background young concurrent copying GC freed 865(144KB) AllocSpace objects, 12(79MB) LOS objects, 4% free, 99MB/103MB, paused 8.174ms,3.734ms total 142.960ms
I/converus.mobile(26626): Background young concurrent copying GC freed 579(112KB) AllocSpace objects, 1(8104KB) LOS objects, 0% free, 146MB/146MB, paused 3.676ms,2.130ms total 218.960ms
I/converus.mobile(26626): Background concurrent copying GC freed 1289(155KB) AllocSpace objects, 21(138MB) LOS objects, 25% free, 71MB/95MB, paused 40us,60us total 106.555ms
I/converus.mobile(26626): Background concurrent copying GC freed 1237(157KB) AllocSpace objects, 16(83MB) LOS objects, 28% free, 59MB/83MB, paused 14.640ms,22us total 170.314ms
I/converus.mobile(26626): Background concurrent copying GC freed 453(120KB) AllocSpace objects, 16(114MB) LOS objects, 26% free, 67MB/91MB, paused 68us,437us total 275.481ms
I/converus.mobile(26626): Background young concurrent copying GC freed 1410(173KB) AllocSpace objects, 12(79MB) LOS objects, 0% free, 146MB/146MB, paused 41.107ms,62us total 265.016ms
I/converus.mobile(26626): Background young concurrent copying GC freed 562(143KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 83MB/83MB, paused 7.956ms,27us total 53.511ms
I/converus.mobile(26626): Background concurrent copying GC freed 929(153KB) AllocSpace objects, 16(118MB) LOS objects, 35% free, 44MB/68MB, paused 14.935ms,22us total 95.028ms
I/converus.mobile(26626): Background young concurrent copying GC freed 754(162KB) AllocSpace objects, 8(43MB) LOS objects, 0% free, 123MB/123MB, paused 18.890ms,57us total 161.656ms
Lost connection to device.

Exited.

Flutter Doctor output

Doctor output
flutter doctor -v
[✓] Flutter (Channel beta, 3.19.0-0.4.pre, on macOS 14.1.1 23B81 darwin-arm64, locale en-US)
    • Flutter version 3.19.0-0.4.pre on channel beta at /Users/jamesstevens/Development/MOBILE DEV/SOURCE/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b7e7d46a04 (8 weeks ago), 2024-02-02 08:21:06 -0600
    • Engine revision 98820f0a77
    • Dart version 3.3.0 (build 3.3.0-279.3.beta)
    • DevTools version 2.31.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0-rc2)
    • Android SDK at /Users/jamesstevens/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0-rc2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /Applications/Brave Browser.app/Contents/MacOS/Brave Browser

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • SM G781U1 (mobile) • RFCN90BNDBB • android-arm64  • Android 13 (API 33)
    • macOS (desktop)    • macos       • darwin-arm64   • macOS 14.1.1 23B81 darwin-arm64
    • Chrome (web)       • chrome      • web-javascript • Brave Browser 121.1.62.165
    ! Error: Browsing on the local area network for James’s Test iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this
      Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for James’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources
    • All expected network resources are available.

• No issues found!
@huycozy huycozy added the in triage Presently being triaged by the triage team label Mar 29, 2024
@huycozy
Copy link
Member

huycozy commented Mar 29, 2024

Hi @jamesstevensdev
I checked this issue using below complete sample code below (save this sample code to a new file at the plugin example lib directory), and running it in profile mode on Android device. After 60 seconds, memory usage seems to be stable (it's down) and the app is not crashed:

Screenshot 2024-03-29 at 14 02 29

Sample code
// Assign the format group based on the platform
import 'dart:io';

import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:flutter/material.dart';

import 'camera_controller.dart';

void main() {
  runApp(const MaterialApp(home: Home()));
}

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _onClick();
        },
      ),
    );
  }

  void _onClick() async {
    final formatGroup = Platform.isIOS ? ImageFormatGroup.bgra8888 : ImageFormatGroup.yuv420;

// Set the target camera resolution
    ResolutionPreset resolutionPreset = ResolutionPreset.ultraHigh;

// Get the correct camera lens
    final cameras = await availableCameras();
    final description = cameras.firstWhere(
      (c) => c.lensDirection == CameraLensDirection.back,
    );

// Create a camera controller
    final CameraController controller = CameraController(
      description,
      resolutionPreset,
      enableAudio: false,
      imageFormatGroup: formatGroup,
    );

// Initialize the controller
    await controller.initialize();

// Start the image stream with an empty callback
    await controller.startImageStream((image) {
      return;
    });

// Wait for 60 seconds
    await Future.delayed(const Duration(seconds: 60));

// Stop and dispose the camera controller
    await controller.stopImageStream();
    await controller.dispose();
  }
}

I'm not sure if your sample code is the same or not. Could you retry and confirm?

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 29, 2024
@jamesstevensdev
Copy link
Author

@huycozy Hello, the code is the same. The memory crash doesn’t happen every time. Additionally, without the camerax package the camera package never caused these sorts of spikes. Often the types of phones used by our customers will have a small amount of RAM and so even spiking to 2GB is enough to cause a crash. I tested this on 3 different Android phones (low and high end phones). Let me know if you would like more information. If needed I can send you a larger code sample to test.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 29, 2024
@huycozy
Copy link
Member

huycozy commented Apr 1, 2024

Checking this a few times, the crash ("Lost connection to device") also occurs to me as well. Checked this on Realme 6, Android 11 with sample code as above #145893 (comment).

Output log
✓  Built build/app/outputs/flutter-apk/app-profile.apk (19.8MB).
Debug service listening on ws://127.0.0.1:57085/srzT7gmuaLQ=/ws
V/PhoneWindow( 9964): DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@ba18da6, this = DecorView@15613e7[MainActivity]
D/ViewRootImpl( 9964): enqueueInputEventMotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=920.0, y[0]=2123.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3401619, downTime=3401619, deviceId=-1, source=0x1002, displayId=0 }
D/ViewRootImpl[MainActivity]( 9964): processMotionEvent MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=920.0, y[0]=2123.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3401619, downTime=3401619, deviceId=-1, source=0x1002, displayId=0 }
D/ViewRootImpl[MainActivity]( 9964): dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=920.0, y[0]=2123.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3401619, downTime=3401619, deviceId=-1, source=0x1002, displayId=0 }
D/ViewRootImpl[MainActivity]( 9964): processMotionEvent MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=920.0, y[0]=2123.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3401699, downTime=3401619, deviceId=-1, source=0x1002, displayId=0 }
D/ViewRootImpl[MainActivity]( 9964): dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=920.0, y[0]=2123.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=3401699, downTime=3401619, deviceId=-1, source=0x1002, displayId=0 }
I/OplusCameraManager( 9964): saveOpPackageName, mOpPackageName: io.flutter.plugins.cameraxexample
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/OplusCameraManagerGlobal( 9964): Connecting to camera service
I/CameraManagerGlobal( 9964): Connecting to camera service
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/chatty  ( 9964): uid=10331(io.flutter.plugins.cameraxexample) CameraX-core_ca identical 4 lines
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
D/CameraRepository( 9964): Added camera: 0
I/Camera2CameraInfo( 9964): Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_3
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=0
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=1
D/CameraRepository( 9964): Added camera: 1
I/Camera2CameraInfo( 9964): Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_3
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=0
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=1
D/CameraValidator( 9964): Verifying camera lens facing on RMX2001L1, lensFacingInteger: null
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=270, isOppositeFacing=false, result=270
I/BufferQueueConsumer( 9964): [](id:26ec00000000,api:0,p:-1,c:9964) connect(): controlledByApp=true
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
I/chatty  ( 9964): uid=10331(io.flutter.plugins.cameraxexample) identical 3 lines
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/DynamicRangeResolver( 9964): Resolved dynamic range for use case androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c275 to no compatible HDR dynamic ranges.
D/DynamicRangeResolver( 9964): DynamicRange@5f420fe{encoding=UNSPECIFIED, bitDepth=0}
D/DynamicRangeResolver( 9964): ->
D/DynamicRangeResolver( 9964): DynamicRange@b9fd4b9{encoding=SDR, bitDepth=8}
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/DeferrableSurface( 9964): Surface created[total_surfaces=1, used_surfaces=0](androidx.camera.core.processing.SurfaceEdge$SettableSurface@d73a05f}
D/DeferrableSurface( 9964): Surface created[total_surfaces=2, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@bdbe37b}
D/DeferrableSurface( 9964): New surface in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@bdbe37b}
D/DeferrableSurface( 9964): use count+1, useCount=1 androidx.camera.core.SurfaceRequest$2@bdbe37b
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/ImageCapture( 9964): createPipeline(cameraId: 0, streamSpec: StreamSpec{resolution=3840x2160, dynamicRange=DynamicRange@b9fd4b9{encoding=SDR, bitDepth=8}, expectedFrameRateRange=[0, 0], implementationOptions=androidx.camera.camera2.impl.Camera2ImplConfig@4bb3c57})
I/BufferQueueConsumer( 9964): [](id:26ec00000001,api:0,p:-1,c:9964) connect(): controlledByApp=true
D/DeferrableSurface( 9964): Surface created[total_surfaces=3, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@2656344}
I/BufferQueueConsumer( 9964): [](id:26ec00000002,api:0,p:-1,c:9964) connect(): controlledByApp=true
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032 ACTIVE
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
I/BufferQueueConsumer( 9964): [](id:26ec00000003,api:0,p:-1,c:9964) connect(): controlledByApp=true
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
D/UseCaseAttachState( 9964): Active and attached use case: [] for camera: 0
D/DeferrableSurface( 9964): Surface created[total_surfaces=4, used_surfaces=1](androidx.camera.core.impl.ImmediateSurface@6f76fb0}
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1743708(32MB) AllocSpace objects, 0(0B) LOS objects, 49% free, 3333KB/6667KB, paused 174us total 111.500ms
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389 ACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032 ACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182 INACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [] for camera: 0
D/UseCaseAttachState( 9964): Active and attached use case: [] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use cases [androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] now ATTACHED
D/UseCaseAttachState( 9964): All use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] for camera: 0
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Resetting Capture Session
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Releasing session in state INITIALIZED
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Attempting to force open the camera.
D/CameraStateRegistry( 9964): tryOpenCamera(Camera@355a1bc[id=0]) [Available Cameras: 1, Already Open: false (Previous state: null)] --> SUCCESS
D/CameraStateRegistry( 9964): Recalculating open cameras:
D/CameraStateRegistry( 9964): Camera                                       State                 
D/CameraStateRegistry( 9964): -------------------------------------------------------------------
D/CameraStateRegistry( 9964): Camera@2b57fa7[id=1]                         UNKNOWN               
D/CameraStateRegistry( 9964): Camera@355a1bc[id=0]                         OPENING               
D/CameraStateRegistry( 9964): -------------------------------------------------------------------
D/CameraStateRegistry( 9964): Open count: 1 (Max allowed: 1)
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Opening camera.
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Transitioning camera internal state: INITIALIZED --> OPENING
D/CameraStateMachine( 9964): New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/CameraStateMachine( 9964): Publishing new public camera state CameraState{type=OPENING, error=null}
D/UseCaseAttachState( 9964): All use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] for camera: 0
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/chatty  ( 9964): uid=10331(io.flutter.plugins.cameraxexample) CameraX-core_ca identical 1 line
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
D/CameraOrientationUtil( 9964): getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=true, result=90
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/OplusCameraManagerGlobal( 9964): setClientInfo, packageName: io.flutter.plugins.cameraxexample, uid: 10331, pid: 9964
I/OplusCameraUtils( 9964): current activityName: io.flutter.plugins.cameraxexample.MainActivity
I/OplusCameraUtils( 9964): getComponentName, componentName: io.flutter.plugins.cameraxexample/io.flutter.plugins.cameraxexample.MainActivity, packageName:io.flutter.plugins.cameraxexample, activityName:io.flutter.plugins.cameraxexample.MainActivity
E/CameraManagerGlobal( 9964): Camera 5 is not available. Ignore physical camera status change
E/CameraManagerGlobal( 9964): Camera 6 is not available. Ignore physical camera status change
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=0
I/CameraManagerGlobal( 9964): postSingleUpdate  onCameraAvailable=0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389 ACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032 ACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182 INACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Use case androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182 ACTIVE
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] for camera: 0
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} CameraDevice.onOpened()
D/Camera2CameraImpl( 9964): {Camera@355a1bc[id=0]} Transitioning camera internal state: OPENING --> OPENED
D/CameraStateRegistry( 9964): Recalculating open cameras:
D/CameraStateRegistry( 9964): Camera                                       State                 
D/CameraStateRegistry( 9964): -------------------------------------------------------------------
D/CameraStateRegistry( 9964): Camera@2b57fa7[id=1]                         UNKNOWN               
D/CameraStateRegistry( 9964): Camera@355a1bc[id=0]                         OPEN                  
D/CameraStateRegistry( 9964): -------------------------------------------------------------------
D/CameraStateRegistry( 9964): Open count: 1 (Max allowed: 1)
D/CameraStateMachine( 9964): New public camera state CameraState{type=OPEN, error=null} from OPEN and null
D/CameraStateMachine( 9964): Publishing new public camera state CameraState{type=OPEN, error=null}
D/UseCaseAttachState( 9964): All use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] for camera: 0
D/UseCaseAttachState( 9964): Active and attached use case: [androidx.camera.core.ImageCapture-930c0a3e-5bcb-4c5b-96d2-7a613838f96667328032, androidx.camera.core.Preview-10cfc2ee-72fb-4a6a-93c5-551d9974c2758071389, androidx.camera.core.ImageAnalysis-b28daf6c-3d09-42c5-9e9d-c6d3c80549118289182] for camera: 0
D/SyncCaptureSessionBase( 9964): [androidx.camera.camera2.internal.SynchronizedCaptureSessionBaseImpl@a806c41] getSurface...done
D/CaptureSession( 9964): Opening capture session.
D/DeferrableSurface( 9964): New surface in use[total_surfaces=4, used_surfaces=2](androidx.camera.core.processing.SurfaceEdge$SettableSurface@d73a05f}
D/DeferrableSurface( 9964): use count+1, useCount=1 androidx.camera.core.processing.SurfaceEdge$SettableSurface@d73a05f
D/DeferrableSurface( 9964): New surface in use[total_surfaces=4, used_surfaces=3](androidx.camera.core.impl.ImmediateSurface@2656344}
D/DeferrableSurface( 9964): use count+1, useCount=1 androidx.camera.core.impl.ImmediateSurface@2656344
D/DeferrableSurface( 9964): New surface in use[total_surfaces=4, used_surfaces=4](androidx.camera.core.impl.ImmediateSurface@6f76fb0}
D/DeferrableSurface( 9964): use count+1, useCount=1 androidx.camera.core.impl.ImmediateSurface@6f76fb0
I/BufferQueueProducer( 9964): [SurfaceTexture-0-9964-0](id:26ec00000000,api:4,p:1118,c:9964) connect(): api=4 producerControlledByApp=true
I/BufferQueueProducer( 9964): [ImageReader-3840x2160f100m4-9964-0](id:26ec00000001,api:4,p:1118,c:9964) connect(): api=4 producerControlledByApp=false
I/BufferQueueProducer( 9964): [ImageReader-3840x2160f23m4-9964-1](id:26ec00000002,api:4,p:1118,c:9964) connect(): api=4 producerControlledByApp=false
D/CaptureSession( 9964): Attempting to send capture request onConfigured
D/CaptureSession( 9964): Issuing request for session.
D/Camera2CaptureRequestBuilder( 9964): createCaptureRequest
D/CaptureSession( 9964): CameraCaptureSession.onConfigured() mState=OPENED
D/CaptureSession( 9964): CameraCaptureSession.onReady() OPENED
I/.cameraxexampl( 9964): Background concurrent copying GC freed 409(88KB) AllocSpace objects, 2(23MB) LOS objects, 34% free, 46MB/70MB, paused 267us total 108.698ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 667(106KB) AllocSpace objects, 6(27MB) LOS objects, 0% free, 105MB/105MB, paused 17.702ms total 140.412ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 434(108KB) AllocSpace objects, 9(47MB) LOS objects, 0% free, 113MB/113MB, paused 14.536ms total 90.357ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 430(113KB) AllocSpace objects, 15(110MB) LOS objects, 27% free, 62MB/86MB, paused 42us total 110.536ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 586(129KB) AllocSpace objects, 12(79MB) LOS objects, 23% free, 78MB/102MB, paused 5.330ms total 53.786ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 412(108KB) AllocSpace objects, 10(55MB) LOS objects, 0% free, 105MB/105MB, paused 5.299ms total 56.821ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 643(132KB) AllocSpace objects, 16(102MB) LOS objects, 22% free, 82MB/106MB, paused 6.610ms total 68.273ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 953(122KB) AllocSpace objects, 15(110MB) LOS objects, 24% free, 74MB/98MB, paused 8.114ms total 56.992ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 680(132KB) AllocSpace objects, 14(102MB) LOS objects, 21% free, 90MB/114MB, paused 5.048ms total 98.796ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 699(132KB) AllocSpace objects, 17(122MB) LOS objects, 17% free, 113MB/137MB, paused 8.216ms total 147.192ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 835(138KB) AllocSpace objects, 13(87MB) LOS objects, 18% free, 105MB/129MB, paused 5.110ms total 46.473ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 611(113KB) AllocSpace objects, 18(110MB) LOS objects, 18% free, 105MB/129MB, paused 9.029ms total 131.881ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 542(114KB) AllocSpace objects, 14(91MB) LOS objects, 22% free, 82MB/106MB, paused 1.943ms total 104.174ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 2026(172KB) AllocSpace objects, 6(31MB) LOS objects, 0% free, 121MB/121MB, paused 5.577ms total 45.898ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 932(122KB) AllocSpace objects, 18(126MB) LOS objects, 17% free, 113MB/137MB, paused 12.860ms total 157.656ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 615(138KB) AllocSpace objects, 12(79MB) LOS objects, 8% free, 90MB/98MB, paused 5.450ms total 49.171ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 402(123KB) AllocSpace objects, 9(47MB) LOS objects, 0% free, 90MB/90MB, paused 5.134ms total 43.901ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 545(129KB) AllocSpace objects, 13(87MB) LOS objects, 22% free, 82MB/106MB, paused 4.927ms total 101.877ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 1920(173KB) AllocSpace objects, 10(55MB) LOS objects, 0% free, 137MB/137MB, paused 6.196ms total 98.214ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1045(121KB) AllocSpace objects, 15(110MB) LOS objects, 21% free, 90MB/114MB, paused 15.813ms total 101.375ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 643(117KB) AllocSpace objects, 16(102MB) LOS objects, 21% free, 90MB/114MB, paused 5.194ms total 114.065ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 443(123KB) AllocSpace objects, 10(55MB) LOS objects, 7% free, 105MB/114MB, paused 5.368ms total 45.432ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 570(112KB) AllocSpace objects, 14(102MB) LOS objects, 21% free, 90MB/114MB, paused 16.813ms total 109.108ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 441(139KB) AllocSpace objects, 9(47MB) LOS objects, 0% free, 113MB/114MB, paused 5.350ms total 73.632ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 565(112KB) AllocSpace objects, 16(118MB) LOS objects, 21% free, 90MB/114MB, paused 15.614ms total 127.312ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 2755(233KB) AllocSpace objects, 22(134MB) LOS objects, 40% free, 34MB/58MB, paused 5.108ms total 199.667ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 641(123KB) AllocSpace objects, 12(79MB) LOS objects, 18% free, 86MB/106MB, paused 5.065ms total 42.161ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 495(112KB) AllocSpace objects, 16(102MB) LOS objects, 24% free, 74MB/98MB, paused 6.488ms total 91.346ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 405(108KB) AllocSpace objects, 9(47MB) LOS objects, 0% free, 105MB/105MB, paused 8.346ms total 47.287ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 468(123KB) AllocSpace objects, 10(55MB) LOS objects, 0% free, 105MB/106MB, paused 5.559ms total 42.262ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 555(112KB) AllocSpace objects, 14(102MB) LOS objects, 25% free, 70MB/94MB, paused 5.014ms total 56.909ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 354(107KB) AllocSpace objects, 9(47MB) LOS objects, 0% free, 113MB/113MB, paused 5.314ms total 75.680ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 401(114KB) AllocSpace objects, 15(110MB) LOS objects, 19% free, 98MB/122MB, paused 61us total 172.796ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 689(116KB) AllocSpace objects, 15(110MB) LOS objects, 22% free, 82MB/106MB, paused 4.191ms total 145.213ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 508(123KB) AllocSpace objects, 10(55MB) LOS objects, 0% free, 113MB/113MB, paused 5.743ms total 54.443ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 601(113KB) AllocSpace objects, 14(102MB) LOS objects, 19% free, 97MB/121MB, paused 77us total 143.185ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 602(112KB) AllocSpace objects, 16(110MB) LOS objects, 22% free, 82MB/106MB, paused 2.892ms total 125.388ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 2783(204KB) AllocSpace objects, 16(102MB) LOS objects, 21% free, 90MB/114MB, paused 8.590ms total 188.504ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 311(108KB) AllocSpace objects, 7(47MB) LOS objects, 7% free, 105MB/114MB, paused 170us total 111.434ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 680(128KB) AllocSpace objects, 17(110MB) LOS objects, 17% free, 113MB/137MB, paused 71.302ms total 936.910ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 934(153KB) AllocSpace objects, 20(118MB) LOS objects, 17% free, 113MB/137MB, paused 100us total 577.080ms
I/.cameraxexampl( 9964): Waiting for a blocking GC Alloc
I/.cameraxexampl( 9964): Background concurrent copying GC freed 3546(228KB) AllocSpace objects, 24(174MB) LOS objects, 23% free, 78MB/102MB, paused 28.002ms total 981.597ms
I/.cameraxexampl( 9964): WaitForGcToComplete blocked Alloc on HeapTrim for 520.767ms
I/.cameraxexampl( 9964): Starting a blocking GC Alloc
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 1295(149KB) AllocSpace objects, 18(110MB) LOS objects, 4% free, 98MB/102MB, paused 2.155ms total 180.011ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1196(156KB) AllocSpace objects, 15(110MB) LOS objects, 17% free, 113MB/137MB, paused 2.661ms total 152.778ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 835(122KB) AllocSpace objects, 13(87MB) LOS objects, 18% free, 105MB/129MB, paused 4.907ms total 146.448ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 2107(167KB) AllocSpace objects, 20(130MB) LOS objects, 19% free, 101MB/125MB, paused 3.264ms total 126.661ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 510(131KB) AllocSpace objects, 13(83MB) LOS objects, 0% free, 129MB/129MB, paused 3.289ms total 100.568ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 1128(139KB) AllocSpace objects, 13(87MB) LOS objects, 0% free, 113MB/113MB, paused 7.322ms total 114.670ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 805(146KB) AllocSpace objects, 18(126MB) LOS objects, 18% free, 105MB/129MB, paused 3.171ms total 187.321ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 1764(170KB) AllocSpace objects, 14(91MB) LOS objects, 20% free, 94MB/118MB, paused 6.835ms total 118.073ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 753(140KB) AllocSpace objects, 5(27MB) LOS objects, 0% free, 129MB/129MB, paused 5.253ms total 71.197ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1015(148KB) AllocSpace objects, 20(122MB) LOS objects, 17% free, 109MB/133MB, paused 4.563ms total 194.399ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1352(130KB) AllocSpace objects, 9(71MB) LOS objects, 20% free, 94MB/118MB, paused 6.294ms total 163.296ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 852(139KB) AllocSpace objects, 12(79MB) LOS objects, 13% free, 101MB/118MB, paused 14.387ms total 94.436ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1251(149KB) AllocSpace objects, 18(114MB) LOS objects, 19% free, 98MB/122MB, paused 533us total 293.696ms
I/.cameraxexampl( 9964): Background young concurrent copying GC freed 1599(167KB) AllocSpace objects, 13(87MB) LOS objects, 21% free, 90MB/114MB, paused 9.342ms total 210.376ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 1830(150KB) AllocSpace objects, 23(134MB) LOS objects, 32% free, 50MB/74MB, paused 9.046ms total 280.577ms
I/.cameraxexampl( 9964): Background concurrent copying GC freed 536(105KB) AllocSpace objects, 13(87MB) LOS objects, 22% free, 82MB/106MB, paused 108us total 281.084ms
Lost connection to device.
Failed to send request: {"jsonrpc":"2.0","id":"93","method":"getMemoryUsage","params":{"isolateId":"isolates/759871330662735"}}

Turning on adb logcat, I also see the test app (with ID io.flutter.plugins.cameraxexample) in the list of lowmemorykiller tag:

04-01 11:22:52.605   540   540 E lowmemorykiller: [22875]  1000     17575      8447             0 com.coloros.deepthinker
04-01 11:22:52.605   540   540 E lowmemorykiller: [22898]  1000     17528      8539             0 com.heytap.mcs
04-01 11:22:52.605   540   540 E lowmemorykiller: [22869]  1000     17423      8509             0 com.oplus.crashbox
04-01 11:22:52.606   540   540 E lowmemorykiller: [22881] 10108     16690      8518             0 com.coloros.exserviceui
04-01 11:22:52.606   540   540 E lowmemorykiller: [22378] 10331    175398    324319             0 io.flutter.plugins.cameraxexample
04-01 11:22:52.606   540   540 E lowmemorykiller: [10650]  1000      3810      9719             0 com.coloros.securitypermission
flutter doctor -v (stable and master)
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.1 23B74 darwin-x64, locale en-VN)
    • Flutter version 3.19.5 on channel stable at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 300451adae (25 hours ago), 2024-03-27 21:54:07 -0500
    • Engine revision e76c956498
    • Dart version 3.3.3
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /Applications/Android Studio.app/
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • Pixel 7 (mobile) • 2B171FDH20084L • android-arm64  • Android 14 (API 34)
    • macOS (desktop)  • macos          • darwin-x64     • macOS 14.1 23B74 darwin-x64
    • Chrome (web)     • chrome         • web-javascript • Google Chrome 123.0.6312.86

[✓] Network resources
    • All expected network resources are available.

• No issues found!
[!] Flutter (Channel master, 3.22.0-1.0.pre.37, on macOS 14.1 23B74 darwin-x64, locale en-VN)
    • Flutter version 3.22.0-1.0.pre.37 on channel master at /Users/huynq/Documents/GitHub/flutter_master
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision fd8561a917 (6 hours ago), 2024-03-31 17:12:24 -0400
    • Engine revision 4f6b832c8e
    • Dart version 3.5.0 (build 3.5.0-5.0.dev)
    • DevTools version 2.34.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /Applications/Android Studio.app/
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.87.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.84.0

[✓] Connected device (3 available)
    • iPad (mobile)   • 00008103-000A1464346A201E • ios            • iOS 17.2 21C62
    • macOS (desktop) • macos                     • darwin-x64     • macOS 14.1 23B74 darwin-x64
    • Chrome (web)    • chrome                    • web-javascript • Google Chrome 123.0.6312.87

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@huycozy huycozy added platform-android Android applications specifically p: camera The camera plugin package flutter/packages repository. See also p: labels. perf: memory Performance issues related to memory has reproducible steps The issue has been confirmed reproducible and is ready to work on c: fatal crash Crashes that terminate the process team-android Owned by Android platform team found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 and removed in triage Presently being triaged by the triage team labels Apr 1, 2024
@reidbaker reidbaker added the P1 High-priority issues at the top of the work list label Apr 1, 2024
@camsim99
Copy link
Contributor

camsim99 commented Apr 1, 2024

I ran the sample code and was able to see similar behavior* to that described in the memory profiler. I have two immediate thoughts:

  1. This may have to do with how garbage collection works for the wrapped instances of PlaneProxys and ImageProxys which are the objects we receive on the native side by starting image streaming via ImageAnalysis. I noticed these are responsible for most of the allocations coming directly from the plugin in my own testing and in the screenshot from the memory profiler above.
  2. The image queue depth for ImageAnalysis should be set to 1 to keep the CameraX plugin consistent with camera_android. I will do this regardless for that reason (unless there's an issue that comes from this of course), but it should also help reduce memory usage as per the documentation. Just tested this out and it seems to help already :)

*I see the memory greatly increasing but do not witness the crash.

@camsim99
Copy link
Contributor

camsim99 commented Apr 1, 2024

  1. The image queue depth for ImageAnalysis should be set to 1 to keep the CameraX plugin consistent with camera_android. I will do this regardless for that reason (unless there's an issue that comes from this of course), but it should also help reduce memory usage as per the documentation. Just tested this out and it seems to help already :)

Hmm actually after reading https://developer.android.com/media/camera/camerax/analyze#operating-modes, I'm under the impression that using the default mode (STRATEGY_KEEP_ONLY_LATEST) should be operating as if we set an image queue depth of 1, so that won't help here. Tested it again and didn't see that big of a decrease.

@camsim99
Copy link
Contributor

camsim99 commented Apr 3, 2024

@jamesstevensdev Which devices did you notice this issue on? I edited my comment above, but I haven't actually seen the crash, so still working on reproducing and wondering if the device types are relevant.

@jamesstevensdev
Copy link
Author

@camsim99 I have reproduced the crash on the Samsung A10, Google Pixel 4, and a Poco phone. However, occasionally, I noticed that the phone did not crash. The memory would spike to about 2GB and then drop back down. This was a rare occurrence. Most of the time, the phone would crash. Feel free to let me know if there is anything I can do to help reproduce this bug for you. I can send videos if needed.

@camsim99
Copy link
Contributor

camsim99 commented Apr 4, 2024

Thanks @jamesstevensdev! I tried this on a Pixel 3A and got the crash to repro and I see a lot more memory being used leading to the crash than I was able to repro with Pixel emulators (which don't crash).

@camsim99
Copy link
Contributor

camsim99 commented Apr 4, 2024

This may have to do with how garbage collection works for the wrapped instances of PlaneProxys and ImageProxys which are the objects we receive on the native side by starting image streaming via ImageAnalysis. I noticed these are responsible for most of the allocations coming directly from the plugin in my own testing and in #145893 (comment).

Took a look into this theory with @bparrishMines and he was able to point be more towards the cause: The InstanceManager (which manages the wrapped native objects used in the plugin) currently deletes garbage collected weak references to native objects it collects every 30 seconds (as defined by a constant). This appears to be too slow for the case of image streaming; when deleting those references every second instead, the app does not crash. It still uses higher memory at times than camera_android in my brief testing, though, which I assume is still to be expected because of the wrapped objects (note that the pink dot represents where image streaming began):

camera_android_camerax
Screenshot 2024-04-04 at 1 36 24 PM

camera_android
Screenshot 2024-04-04 at 2 47 24 PM

To move forward here, I plan to play around with that time interval and at least briefly look into how we may be able to use Android's tools for managing memory to dynamically change the interval based on a suggestion by Maurice. @bparrishMines please feel free to add any context I missed!

@gau-nernst
Copy link

@camsim99 changing CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL to 1000 fix the memory increase issue for me. Thank you. Do you know if a fix for this is coming soon to a release version?

@camsim99 camsim99 added this to In progress in Android Camera Re-write Apr 5, 2024
@camsim99
Copy link
Contributor

camsim99 commented Apr 5, 2024

@gau-nernst Happy you were able to confirm that! I'm actively working on this, so yes. Likely in the next week or two.

@camsim99
Copy link
Contributor

camsim99 commented Apr 5, 2024

To move forward here, I plan to play around with that time interval and at least briefly look into how we may be able to use Android's tools for managing memory to dynamically change the interval based on a suggestion by Maurice. @bparrishMines please feel free to add any context I missed!

I've looked into:

  1. Using onTrimMemory to dynamically change the interval. I may look into this deeper but it will require wrapping the FlutterActivity in some way to implement onTrimMemory or forwarding the onTrimMemory callback made on the FlutterActivity to the plugin somehow. It is currently forwarded to the framework and exposed there at the Widget level via WidgetsBndingObserver I believe. This may be more feasible as a longer term solution for Dart-wrapped native objects in general, as I don't believe that work should block making CameraX usable.
  2. Dynamically changing the interval based on the number of weak instances the InstanceManager holds when the call to remove instances is made. This works, but I think this can be dangerous since this approach would be based on an arbitrary value for how many reference we consider "too much".
  3. Adjusting the time interval any time image streaming is started/stopped. This seems to work well when an additional call to clear the weak references before image streaming starts is made. Thinking this might be the best option to keep the changes contained to image streaming in case they impact performance. @bparrishMines any concerns with this approach?

Other than these, we know we can just lower the interval for the entire plugin.

@bparrishMines
Copy link
Contributor

For solution 1, we would want to listen to the callback on the Java side. But since it seems to be supported by WidgetsBndingObserver, this solution would require changes to the engine. This callback is probably useful for plugins, but I think this would require a longer discussion about supporting it.

For solution 2, the problem is not necessarily the number of instances in memory, but how much memory they take up. For image streaming, it looks like the objects stuck in memory were actually the PlaneProxy. So I think the Dart code was actually holding on to full unused images, waiting for the Java code to give the green light to release them. I agree that this isn't a great solution since "too many" depends on what the reference is.

I'm fine with implementing solution 3 since we already know that lowering the variable prevents the crash. You can also probably lower the default frequency to 3000 since this is what is used in webview_flutter_android: https://github.com/flutter/packages/blob/main/packages/webview_flutter/webview_flutter_android/android/src/main/java/io/flutter/plugins/webviewflutter/InstanceManager.java#L39. The 30000 in camera_android_camerax might have been an accidental copy/paste mistake.

auto-submit bot pushed a commit to flutter/packages that referenced this issue Apr 10, 2024
…ped native objects (#6493)

This PR:

1. Shortens the `InstanceManager`'s default time interval that it waits to remove references to Dart-wrapped Android native objects. The shortened interval matches `webview_flutter_android`s `InstanceManager`; because it has been tested by that plugin, we expect it not to impact performance and this could help reduce overall memory usage by the plugin.
2. Dynamically shortens that same time interval whenever image streaming is started/stopped to account for the increased memory usage that this camera use case requires.

Fixes flutter/flutter#145893.
@huycozy huycozy added the r: fixed Issue is closed as already fixed in a newer version label Apr 11, 2024
@camsim99 camsim99 moved this from In progress to Done in Android Camera Re-write Apr 19, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: fatal crash Crashes that terminate the process found in release: 3.19 Found to occur in 3.19 found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: camera The camera plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. perf: memory Performance issues related to memory platform-android Android applications specifically r: fixed Issue is closed as already fixed in a newer version team-android Owned by Android platform team
6 participants