SwiftData @ModelActor Memory usage skyrocketing when changing properties

When changing a property of a SwiftData Model from a ModelActor the memory needed slightly increases. Once you do that more often, you can see that the usage is linearly increasing. I modified the Swiftdata template as little as possible. This is the least code I need to reproduce the problem:

Changes In the @main struct :

ContentView(modelContainer: sharedModelContainer)

ContentView:

struct ContentView: View {
    @Query private var items: [Item]
    let dataHanndler: DataHandler
    @State var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { t in })
    var body: some View {
        NavigationSplitView {
            List {
                ForEach(items) { item in
                    NavigationLink {
                        Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
                    } label: {
                        Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
                    }
                }
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
                ToolbarItem {
                    Button {
                        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { t in
                            Task {
                                await dataHanndler.updateRandom()
                                // Obviously this makes little sense but I need to update a lot of entities in my actual app. This is the simplest way to demonstrate that. updateRandom() could also be a function of a view but that doesn't make a difference
                            }
                        }
                    } label: {
                        Label("Do a lot of writing", systemImage: "gauge.with.dots.needle.100percent")
                    }
                }
                ToolbarItem {
                    Button {
                        timer.invalidate()
                    } label: {
                        Label("Invalidate", systemImage: "stop.circle")
                    }

                }
                
            }
        } detail: {
            Text("Select an item")
        }
    }
    
    private func addItem() {
        Task {
            await dataHanndler.insert(timestamp: Date.now)
        }
    }
    
    init(modelContainer: ModelContainer) {
        self.dataHanndler = DataHandler(modelContainer: modelContainer)
    }
}

ModelActor:

@ModelActor
actor DataHandler {
    public func update<T>(_ persistentIdentifier: PersistentIdentifier, keypath: ReferenceWritableKeyPath<Item, T>, to value: T) throws {
        let model = modelContext.model(for: persistentIdentifier) as! Item
        model[keyPath: keypath] = value
    }
    
    public func insert(timestamp: Date) {
        let item = Item(timestamp: timestamp)
        modelContext.insert(item)
    }
    
    
    
    public func updateRandom() {
        let count = try! modelContext.fetchCount(FetchDescriptor<Item>())
        var descriptor = FetchDescriptor<Item>()
        descriptor.fetchOffset = Int.random(in: 0..<count)
        descriptor.fetchLimit = 1
        let model = try! modelContext.fetch(descriptor)
        model.first!.timestamp = Date.now
    }
}

I filed a bug report FB14876920 but I am looking for other ideas to solve this before it will be fixed in a future update. The modelContext I use is created and managed by the @ModelActor macro.

Happy to hear ideas

Your code and model are quite straightforward. If they indeed trigger a memory issue, I can only suggest that you follow up with your feedback report to see what the SwiftData folks have to say.

Before that though, would you mind to share how you observe the memory consumption of your app? I am wondering if the memory consumption eventually goes down, or if it eventually triggers a low-memory crash. If the memory consumption goes down at some point, meaning that the system recycles the memory at some point, then it may not be a real issue.

Regarding your real app, which most likely has a more complicated model schema, you might look at the discussion in the following post to see if that is relevant:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I have a similar issue when updating model attributes in an .onChange event (FB13812446, filed in May this year). Pretty sure that it happens within an .onChange is not relevant. Just checked with the sample project I attached to this FB: Still causes the memory to rise using the latest Xcode beta (Version 16.0 beta 6 (16A5230g)).

SwiftData @ModelActor Memory usage skyrocketing when changing properties
 
 
Q