UIGraphicsImageRenderer memory issue (iOS 17.5.1)

Testing on iPhone 12 mini, I have encountered a weird situation. I am try to take snapshot of my view, which works fine but the memory is never released after the snapshot is taken.

func screenshot(view: UIView, scale:Double) -> URL? {
        guard let containerView = view.superview, let containerSuperview = containerView.superview else { return nil }
                
        let rendererFormat = UIGraphicsImageRendererFormat()
        rendererFormat.scale = scale
        
        var renderer = UIGraphicsImageRenderer(bounds: containerView.frame, format: rendererFormat)

        let image = autoreleasepool {
            return renderer.image { context in
                containerSuperview.drawHierarchy(in: containerSuperview.layer.frame, afterScreenUpdates: true) //memory hog starts from here
            }
        }
                
        guard let data = image.heicData() else {
            return nil
        }

//more code to save data to file URL and return it
}

initially it appears to work normally but as soon as I change the scale: rendererFormat.scale = 10 I can see a spike in memory but the problem is then the memory is never released even after the image is saved. so initially, the app uses: 35MB memory -> when processing the memory usage jumps to expected 250MB to 300MB to process large image -> after processing the memory goes down to around 90MB to 120MB but it never really returns to it's original 35MB state.

Is this a bug or this is expected? If this is expected behaviour then is there any low level API to free the memory after it's job is done.

I would recommend you file a feedback report with a sample project. Without knowing exactly what memory is being allocated its impossible to provide more than a very generic reply (mostly involving using memory tools to verify that you haven't leaked something).

UIGraphicsImageRenderer memory issue (iOS 17.5.1)
 
 
Q