Kotlin Multiplatform - native iOS memory overload issue

I’m creating a kotlin multiplatform application that runs on web, iOS and Android that will process images captured from the device’s video stream. The processing works fine on Android and web, but iOS has a serious memory problem that causes HUGE spikes in the application memory footprint.

on iOS, I have my Camera management class that handles starting the video stream and capturing the output frames as CMSampleBuffer’s. Here is the output function:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        DispatchQueue.main.async { [unowned self] in
            guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
            let ciImage = CIImage(cvPixelBuffer: imageBuffer)
            guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return }
            let uiImage = UIImage(cgImage: cgImage, scale: 1.0, orientation: .up)
           // kotlin class that handles image processing
            self.manager.processImage(image: uiImage)

Capturing the frames works fine, but when I pass the UIImage into the Kotlin function : “self.manager.processImage(image: UIImage)” the memory quickly overloads and creates a saw-tooth pattern that can lead to an app crash.

Here is the processImage function:

class ImageManager {

    fun processImage(image: UIImage) {
        println("processing image")

I’ve only got the function printing out a simple value right now for debugging purposes. But this code causes huge spikes in the memory that reach 600Mb before the garbage collection cycles catch up. This code will eventually crash the application once the memory gets close to the 2Gb mark.

If I comment out the “self.manager.processImage(image: uiImage)” the memory stays around ~40Mb and the application is stable.

I’ve attached 2 images:

  • First is the stable memory when not passing the UIImage into the Kotlin processing function.
  • Second is with the function in use and how much memory the app accumulates in such a short amount of time.

Any thoughts or suggestions on something that might fix this?? This is currently blocking my development and I can’t find a way around it :frowning: