Magic Leap 2 Async JPEG Saving Crash after ~20 Seconds (Using ImageSharp)

Hi all,

I’m developing a custom camera recorder on Magic Leap 2 using Unity and MLCamera APIs. The goal is to record front camera frames at 1920x1080, 20~30fps, and save each frame asynchronously as JPEG using ImageSharp (v2.1.0) on a background thread.

I am not doing any AR rendering—just pure RGB capture and disk saving.

However, the app consistently crashes (no exception, full freeze or exit) after around 15–20 seconds of recording, even at low FPS like 20fps. The crash happens faster at 30fps.

Setup:

  • Unity Editor: 2022.3.22f1
  • Magic Leap Hub: Magic Leap Hub 3
  • Magic Leap 2 OS: 1.12.0
  • Magic Leap SDK (OpenXR): 1.12.0
  • JPEG Encoding Library: SixLabors.ImageSharp v2.1.0
  • Frame format: RGBA_8888
  • Saving thread: custom Thread, writing images and a CSV of timestamps

What I’ve tried:

  • Using ConcurrentQueue<FrameData> for background saving
  • Capped queue size to prevent overload
  • Verified correct disposal of ImageSharp objects and MemoryStreams
  • Tried both Texture2D.EncodeToJPG() and ImageSharp.SaveAsJpeg() (both cause eventual crash)
  • Reduced resolution and JPEG quality
  • Reduced FPS to 20 (still crashes after some time)

Suspected cause:

It seems the continuous memory pressure from raw RGBA frames + encoding buffers + GC load may be causing unrecoverable memory overflow, though the crash happens silently (no logs, no Unity errors).

Questions:

  • Is there any known memory limit or constraint when using MLCamera + continuous capture?
  • Has anyone successfully implemented long-duration camera capture with encoding?
  • Is there a recommended safe approach for high-frequency frame capture and disk saving on Magic Leap 2?
  • Could this be an issue with ImageSharp compatibility or native memory leaks?

Any suggestions or recommended approaches would be appreciated!

Best,
Houze Yang

You are indeed running out of memory when processing the images. This crash would behave the same on other android device when an application consumes too much memory. We do not have a hard memory limit documented but here are the hardware specs for our device:


I would recommend trying to decrease the image resolution or the capture framerate. Since the encoding of the images occurs async, the application is most likely holding a long buffer of images that are still to be processed which could result in this issue. Depending on your use case you could also look into encoding the stream into an h264 video, or streaming it using webrtc or simply limiting the number of images that are stored in the queue .

1 Like