RealityKit Memory Leak

I'm building a visionOS app which loads a Reality Composer scene with a large number of models. The app includes several of these scenes, and allows the user to switch between them. Because the scenes have a large number of models, I want to unload the currently loaded scene before loading a different one. So far I have been unable to reclaim all of the used memory by removing the entities from the scene.

I've made a few small changes to the Mixed Immersive app template which demonstrate this behavior which I've included below (apparently I'm unable to upload a zip file with the entire project). Using just the two spheres included in the reality kit content the leaked memory is fairly small, but if you add a couple larger models to the scene (I was able to easily find free ones online) then the memory leak becomes much more obvious.

When the immersive space is initially opened, I'm seeing roughly 44MB of used memory (as shown in the Xcode Debug navigator). Each time I tap the "Load Models" and then "Unload Models" buttons, the memory use decreases but does not get back down to the initial amount. Subsequent loads and unloads will continue to increase the used memory (the amount of increase will depend on the models that you add to the scene).

Also note that I've seen similar memory increases when dynamically creating the entities. Inside ViewModel.loadModels I've included some commented out code that dynamically creates entities instead of loading a Reality Composer scene.

Is there a way to fully reclaim the used memory? I've tried many different ways to clear the RealityKit entities but so far have been unsuccessful.

struct RKMemTestApp: App {
	private var viewModel = ViewModel()
	
    var body: some Scene {
        WindowGroup {
            ContentView()
				.environment(viewModel)
        }

        ImmersiveSpace(id: "ImmersiveSpace") {
            ImmersiveView()
				.environment(viewModel)
        }
    }
}

Add this above the body in ContentView:

@Environment(ViewModel.self) private var viewModel

The ContentView body should be:

VStack {
            Toggle("Show ImmersiveSpace", isOn: $showImmersiveSpace)
                .font(.title)
                .frame(width: 360)
                .padding(24)
                .glassBackgroundEffect()
			Button("Load Models") {
				viewModel.loadModels()
			}
			Button("Unload Models") {
				viewModel.unloadModels()
			}
        }

ImmersiveView:

struct ImmersiveView: View {
	@Environment(ViewModel.self) private var viewModel
	
    var body: some View {
        RealityView { content in
			if let rootEntity = viewModel.rootEntity {
				content.add(rootEntity)
			}
		} update: { content in
			if viewModel.rootEntity == nil && !content.entities.isEmpty {
				content.entities.removeAll()
			} else if let rootEntity = viewModel.rootEntity, content.entities.isEmpty {
				content.add(rootEntity)
			}
		}
    }
}

ViewModel:

import Foundation
import Observation
import RealityKit
import RealityKitContent

@Observable
class ViewModel {
	var rootEntity: Entity?

	init() {
	}
	
	func loadModels() {
		Task {
			if let scene = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
				Task { @MainActor in
					if rootEntity == nil {
						rootEntity = Entity()
					}
					rootEntity!.addChild(scene)
				}
			}
		}
		
		/*if rootEntity == nil {
			rootEntity = Entity()
		}
		for _ in 0..<1000 {
			let mesh = MeshResource.generateSphere(radius:0.1)
			let material = SimpleMaterial(color: .blue, roughness: 0, isMetallic: true)
			let entity = ModelEntity(mesh: mesh, materials: [material])
			entity.position = [Float.random(in: 0.0..<1.0), Float.random(in: 0.5..<1.5), -Float.random(in: 1.5..<2.5)]
			rootEntity!.addChild(entity)
		}*/
	}
	
	func unloadModels() {
		rootEntity?.children.removeAll()
		rootEntity?.removeFromParent()
		rootEntity = nil
	}
}
RealityKit Memory Leak
 
 
Q