SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.18k stars 225 forks source link

WebImage in LazyVStack take massive memory #271

Open ywwzwb opened 1 year ago

ywwzwb commented 1 year ago

I write a demo with SDWebImageSwiftUI, just put some images in a lazyVStack. here is code for the demo:

struct ContentView: View {
    @State private var ImageURLList: [String] = []
    var body: some View {
        GeometryReader { geometryProxy in
            VStack {
                HStack{
                    Button("test load") {
                        // parse the image list...
                    }
                    Spacer()
                    Button("clear") {
                        ImageURLList.removeAll()
                    }
                }
                ScrollView {
                    LazyVStack(spacing: 0) {
                        ForEach(ImageURLList, id: \.self) { url in
                            ImageBodyView(url: url)
                                .scaledToFit()
                                .frame(width: geometryProxy.size.width)
                        }
                    }
                }
            }
        }
        .padding()
    }
    struct ImageBodyView: View {
        @State var url: String
        var body: some View {
            VStack {
                WebImage(url: URL(string: url))
                    .resizable()
            }
        }
    }
}

this demo using a lots of memories, and it will keep growing while scrolling to the bottom. The memory will not release before I clean the imageUrlList.

image

edolorenza commented 1 year ago

Does this only happen on iOS 17 ?

ywwzwb commented 1 year ago

I tested it on iOS16, both on simulator and iPhone device. @edolorenza

dreampiggy commented 1 year ago

Have a try with the latest v3.0.0-Beta or master branch ? Is this still reproducable ?

ywwzwb commented 1 year ago

I have tried 3.0.0-beta with iOS16.6.1 on my iPhone, and seems using less memory than before. image but the memory skill keep growing while scrolling the image list.

ywwzwb commented 1 year ago

Here is a full demo, which will load some images of cat, hope it helps.

struct ImageBodyView: View {
    @State var url: String
    var body: some View {
        VStack {
            WebImage(url: URL(string: url))
                .resizable()
        }
    }
}
struct ContentView: View {
    @State private var imageURLList: [String] = []
    var body: some View {
        GeometryReader { geometryProxy in
            VStack {
                HStack{
                    Button("test load") {
                        self.imageURLList = (1..<100).map{ "https://cataas.com/cat?r="+String($0) }
                    }
                    Spacer()
                    Button("clear") {
                        self.imageURLList.removeAll()
                    }
                }
                ScrollView {
                    LazyVStack(spacing: 0) {
                        ForEach(self.imageURLList, id: \.self) { url in
                            ImageBodyView(url: url)
                                .scaledToFit()
                                .frame(width: geometryProxy.size.width)
                        }
                    }
                }
            }
        }
        .padding()
    }
}
frandelarosa commented 11 months ago

I'm using a LazyVStack for my project and I can confirm that. CPU is between 90% - 98% during the scroll.

Sharrykhan01 commented 8 months ago

Yes it do occur while scrolling the memory raises and unexpectedly gets crashed too. Please if you find something to release out the memory, do inform me too.