How to delete Entity memory in RealityKit

Although the model disappeared, the memory did not decrease. How to delete memory occupied by the model?

// MARK: === viewDidLoad
  override func viewDidLoad() {
    super.viewDidLoad()
     
    arView.renderOptions = [.disableMotionBlur, .disableDepthOfField, .disableCameraGrain, .disableHDR]
    arView.environment.sceneUnderstanding.options.insert(.occlusion)
     
    rootAnchor = AnchorEntity(plane: .horizontal)
    arView.scene.addAnchor(rootAnchor)
     
    var cancellable: AnyCancellable? = nil
    cancellable = Entity.loadAsync(contentsOf: Bundle.main.url(forResource: "vyygabbj_afr", withExtension: "usdz")!)
      .sink(receiveCompletion: { error in
        DispatchQueue.main.async {
         cancellable?.cancel()
         cancellable = nil
        }
      }, receiveValue: { [weak self] ey in
        guard let self = self else { return }

        self.modelEy = ModelEntity()
        self.modelEy.addChild(ey)
         
        self.rootAnchor.addChild(self.modelEy)
        ey.availableAnimations.forEach {
          ey.playAnimation($0.repeat())
        }
         
        DispatchQueue.main.async {
          cancellable?.cancel()
          cancellable = nil
        }
    })
  }
  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
  {
    modelEy.removeFromParent()
  }
Answered by caopengxu in 715295022

problem solved add: modelEy = nil

problem solved add: modelEy = nil

Accepted Answer

problem solved add: modelEy = nil

How to delete Entity memory in RealityKit
 
 
Q