Images retain memory usage

This is a very simple code in which there is only one button to start with. After you click the button, a list of images appear. The issue I have is that when I click on the new button to hide the images, the memory stays the same as when all the images appeared for the first time. As you can see from the images below, when I start the app, it starts with 18.5 mb, when I show the images it jumps to 38.5 mb and remains like that forever. I have tried various way to try and reduce the memory usage but I just can't find a solution that works. Does anyone know how to solve this? Thank you!

import SwiftUI

struct ContentView: View {
    
    @State private var imagesBeingShown = false
    @State var listOfImages = ["ImageOne", "ImageTwo", "ImageThree", "ImageFour", "ImageFive", "ImageSix", "ImageSeven", "ImageEight", "ImageNine", "ImageTen", "ImageEleven", "ImageTwelve", "ImageThirteen", "ImageFourteen", "ImageFifteen", "ImageSixteen", "ImageSeventeen", "ImageEighteen"]
    
    var body: some View {
     
            if !imagesBeingShown {
                
                VStack{
                    Button(action: {
                        imagesBeingShown = true
                    }, label: {
                        Text("Turn True")
                    })
                }
                .padding()
            } else {
                
                VStack {
                    Button(action: {
                        imagesBeingShown  = false
                    }, label: {
                        Text("Turn false")
                    })
                    
                    ScrollView {
                        LazyVStack {
                            ForEach(0..<listOfImages.count, id: \.self) { many in
                                Image(listOfImages[many])
                            }
                        }
                    }
                }
            }
    }
}

Hello lunar-serenade,

I think that has to do with some SwiftUI internal stuff. I think as long as there is more than enough memory available, the images will be kept in cache for a certain amount of time. If I remember correctly, you can "bypass" the cache by using the init(uiImage: UIImage) initializer, and initializing the UIImage with this initializer: init?(contentsOfFile path: String)

Also, I have some functionality in my App which loads a lot of stuff into the memory and often caused my app to crash, because it exceeded the maximum allowed memory usage. After doing a lot of research and debugging, I found out that wrapping all the code (that loads the files into memory) in a try! autoreleasepool { ... } did the trick. To me it seems that the Swift garbage collections doesn't work very reliable. I don't recommend this approach, since garbage collection should be completely handled by Swift itself. But right now this is the only method that worked for me.

Please let us know if this could fix your problem. Thanks!

Images retain memory usage
 
 
Q