dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.11k stars 96 forks source link

Huge peak memory consumption while displaying images #100

Closed lisindima closed 3 years ago

lisindima commented 3 years ago

Summary and/or background The moment I open my list with a large number of images, there is a huge peak in memory consumption. Sometimes this does not happen in the same situation, I could not understand this pattern. I also notice this problem in your demo application for this package. When I choose the option with the largest images.

OS and what device you are using

Version of URLImage library

What you expected would happen Don't have such terrible memory consumption

What actually happens Peak memory consumption, which causes a crash on a real device due to insufficient memory.

Sample code

URLImage(
            url: path,
            options: URLImageOptions(cachePolicy: .returnCacheElseLoad(cacheDelay: nil, downloadDelay: delay)),
            inProgress: { progress in
                ProgressView(value: progress)
                    .progressViewStyle(CircularProgressViewStyle())
            },
            failure: { _, retry in
                Button(action: retry) {
                    Image(systemName: "arrow.clockwise.circle.fill")
                        .imageScale(.large)
                }
            }
        ) { image in
            image
                .resizable()
        }
        .cornerRadius(8)
        .frame(width: size, height: size)

Test data This is reproduced with any images with a resolution greater than 1000x1000 and a weight greater than 1MB.

Additional information:

dmytro-anokhin commented 3 years ago

Memory spikes happen naturally because images are big, this is not necessarily a bug. However, version 2 is missing control over maximum size of a decoded image. Adding this will help reduce memory consumption. I'm working on it.

dmytro-anokhin commented 3 years ago

And thank you for detailed report 👍

dmytro-anokhin commented 3 years ago

Hey,

there's a new version 2.1.4. You can now specify maximum image dimensions in pixels using maxPixelSize property of URLImageOptions. It defaults to 300x300. I picked this number randomly, maybe will change it or introduce some logic based on the device type in the future.

Using maxPixelSize eliminates memory spikes in my tests. I'm closing the issue, but feel free to reopen if you discover an edge case.