0

I am testing the following codes

// firstly list all the assets in the Photos.
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
fetchOptions.predicate = NSPredicate(format: "mediaType = %d || mediaType = %d", PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)

let assets = PHAsset.fetchAssets(with: fetchOptions)
let totalAssetCount = assets.count

for index in 0...(totalAssetCount-1) {
    let asset = assets.object(at: index)

    autoreleasepool{
        var resource = PHAssetResource.assetResources(for: asset)
    }
}

And I can see that the above codes have severe memory leak issue with the following operation (memory usage continues to grow until all the images are looped, and it never goes down even when the loop finishes).

var resource = PHAssetResource.assetResources(for: asset)

Even with the autoreleasepool, the memory never gets released. Also the symptom is gone when I comment out the PHAssetResource.assetResources(). So is there any resource usage issue with the PHAssetResource.assetResources() function in swift? If so, how can I resolve and workaround these problems?

I noticed previous posts here PHAssetResource.assetResources(for: asset).first.OriginalFilename gives nil after 300 assets but apparently adding the autoreleasepool call does not help me at all.

  • This problem could be a mirage. Are you testing in a Release build on a device, using Instruments? Any other way of testing will give you a false picture of memory usage. – matt Nov 25 '20 at 01:53
  • I can see this is still the issue when I am using the Instrument to profile on a release build. The memory keeps going up until the scan of all the assets finishes. – user14230441 Nov 25 '20 at 04:05

0 Answers0