SwiftUI Root View Remains in Memory after Switch

Has anyone figured out how to get SwiftUI to release the memory of a complex view struct which is not being shown?

I'm working on a SwiftUI app where I'd like to switch the root view, from a complex view (which uses a fair bit of memory), to a simpler one (which uses much less memory).

To illustrate this, I've created a basic SwiftUI 2.0 app below which starts with a complex root view and then uses the scene phase transition to switch to a simple "Hello World" view as the app moves into the background.

In simulation, the app starts using 18.5MB of memory to show the complex view. When I sent the app to the background, the root view is switched to the simple view - which should lower the app's memory. Instead the memory footprint goes UP to 19.6MB, which suggests that the complex view isn't being released from memory.

Code Block Swift
import SwiftUI
@main struct TestAppBackgroundingApp: App {
@Environment(\.scenePhase) var scenePhase:ScenePhase
@State var showLongList = true
var body: some Scene {
WindowGroup {
/*Change Root view based on bool*/
if( showLongList ){
LongList()
}else{
SimpleView()
}
}
.onChange(of: scenePhase) { (newScenePhase) in
print("Scene Phase Transition: \(scenePhase) --> \(newScenePhase)")
if( scenePhase == .active && newScenePhase == .inactive ){
/*Toggle Root View as app moves to background*/
print("Hiding long list")
DispatchQueue.main.async {
self.showLongList.toggle()
}
}
}
}
}
struct SimpleView: View {
var body: some View {
Text("SimpleView").font(.largeTitle)
}
}
struct LongList: View {
var body: some View {
VStack{
Text("Long List").font(.largeTitle)
List{
ForEach(1...20_000, id: \.self) { i in
HStack{
Image(systemName: "gift.circle.fill").renderingMode(.original).font(.largeTitle)
Text("Row#\(i)").bold()
}
}
}
}//VStack
}//Body
}


SwiftUI Root View Remains in Memory after Switch
 
 
Q