SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

WebImage in LazyVStack take massive memory

Open ywwzwb opened this issue 1 year ago • 7 comments

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

ywwzwb avatar Jul 24 '23 03:07 ywwzwb

Does this only happen on iOS 17 ?

edolorenza avatar Sep 23 '23 07:09 edolorenza

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

ywwzwb avatar Sep 23 '23 08:09 ywwzwb

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

dreampiggy avatar Sep 23 '23 09:09 dreampiggy

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 avatar Sep 23 '23 12:09 ywwzwb

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()
    }
}

ywwzwb avatar Sep 23 '23 12:09 ywwzwb

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

frandelarosa avatar Oct 31 '23 15:10 frandelarosa

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.

Sharrykhan01 avatar Feb 01 '24 13:02 Sharrykhan01