Massive Memory Leak in SwiftUI for macOS

I've pretty much completed creating my first macOS SwiftUI app. It's working fine but after using it for more than 10 minutes I noticed a dramatic slow down and big memory footprint. I condensed the problem down into the following minimal example, which is a full SwiftUI app for both macOS and iOS.

Code Block language Swift
import SwiftUI
@main
struct TestApp: App {
@State var selection: Int?
var body: some Scene {
WindowGroup {
NavigationView {
List(0...10, id: \.self) { element in
NavigationLink(destination: VeryComplexView(text: "\(element)"),
tag: element, selection: $selection) {
Text("\(element)")
}
}
}
}
}
}
struct VeryComplexView: View {
let text: String
var body: some View {
HStack {
ForEach(1...10, id: \.self) { _ in
List(1...5000, id: \.self) { element in
Text("\(text) \(element)")
}
}
}
}
}


On macOS after just a few clicks in the NavigationView I'm getting a memory footprint of 1GB and performance decreases. Profiling the app with Leak Detector also shows a massive amount of leaks in SwiftUI classes. Compiling the same app for iOS stays at a reasonable footprint of 50 MB even after using it for a while. This bug prevents me from properly using my app. I submitted a bug report in Feedback Assistant about a month ago with no answer or reaction. Please help! How can such a huge leak stay unnoticed.
I'm running the latest Big Sur 11.2.1 and Xcode 12.4

I just pasted your code into Xcode, ran it, and started clicking around. It seems a bit bogged down, because it can take over a second to change the VeryComplexView when I click on the different numbers in the left nav. But, the memory use is staying around 100-150MB. I'm running macOS 11.2.3, so maybe you could try upgrading to that?
Thanks @rnikander,

you're right, the upgrade helped fixing it. Seems like this minimal example was fixed. I still have a problem with my big app, though. Now, seems to be correlated to the toolbar component.

I created a new thread with a new minimal example for this ( https://developer.apple.com/forums/thread/677969 ) Can you check if this is is causing problems on your end?

Johannes

Massive Memory Leak in SwiftUI for macOS
 
 
Q