PHImageManager.requestImage method spiking memory by 20MB while loading a 4MB image

We are developing a widget for an iOS app that displays photos from the user's local photo library. To get photos, we use the PHImageManager.requestImage method with the following parameters:

let requestOptions = PHImageRequestOptions()
requestOptions.isNetworkAccessAllowed = false
requestOptions.isSynchronous = true
requestOptions.resizeMode = .exact
requestOptions.deliveryMode = .highQualityFormat

PHImageManager.requestImage(for: asset, targetSize: context.displaySize /* context is the TimelineProviderContext of the widget */, contentMode: .aspectFill, options: requestOptions) { (image, error) in 
    ...
}

We load about 12 images per update cycle, calling the requestImage method that many times. While executing this code we can regularly monitor that executing the requestImage method increases the memory consumption by 20MB, even while loading an image with a size of about 3MB. (Image size has been retrieved from the PHAssetResource.) The memory spikes before the callback closure is being executed. We tinkered around with displaySize, PHImageRequestOptions and trying to reduce our overall memory consumption, to no avail. We integrated a DispatchGroup in our code to ensure that requesting images is strictly synchronous and do so in an autoreleasepool to free up memory after processing the image data. I like to stress that we a reasonably sure that the memory spike does not occur in our closure that receives the requested image but in the time between calling the requestImage method and the callback. This has been tested by placing a breakpoint in the line where we call requestImage and a breakpoint in the first line of the callback closure. The first breakpoint stops execution of the code, when continuing execution, the process ends immediately and we get a "" warning and never hitting the second breakpoint.

This is a huge problem for us because iOS terminates our widget process every time the memory consumption spikes due to exceeding the 30MB memory limit (our widget consumes about 13-15MB of memory while updating the timeline).

The issue described above was observed on an iPhone 11, iOS 16.4 and occurs while trying to load JPG photos between 0.7-4.0MB.

We kindly ask for help on why memory spikes when using PHImageManager.requestImage, how to prevent this or if this is a known issue.

Accepted Reply

I have not filed such a feedback yet and am unsure what a "Feedback with a repro case" is. Can you please point out how to file such a feedback?

We are seeing the described behaviour with various JPG files. Our app selects random photos from a user's library and tries to access the asset data. The library in question has thousands of photos with various file sizes.

  • Accidentally marked as answer. This thread is not resolved yet!

  • Accidentally marked as answered.

Add a Comment

Replies

Have you filed a Feedback with a repro case by chance? If so please paste the number as that will help engineers take a closer look. Are you seeing this with a wide array of JPG examples or is it occurring only for certain examples or sizes?

I have not filed such a feedback yet and am unsure what a "Feedback with a repro case" is. Can you please point out how to file such a feedback?

We are seeing the described behaviour with various JPG files. Our app selects random photos from a user's library and tries to access the asset data. The library in question has thousands of photos with various file sizes.

  • Accidentally marked as answer. This thread is not resolved yet!

  • Accidentally marked as answered.

Add a Comment

I'm experiencing the same issue. It seems that setting resizeMode to .fast alleviates the problem.