Possible memory leak while creating mesh

Viewed 90

I am using RealityKit to generate mesh after a particular time interval and adding it as child to the root node. Before creating another mesh I am removing the previously created Entity from parent node also assigning plainCard = nil to it.

Below is the sample code:

import RealityKit
import Combine
import SceneKit

private var sceneEventsUpdateSubscription: Cancellable!
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    let anchor = AnchorEntity(world: [0,0,0])
    let rootEntity:Entity = Entity()
    var plainCard:ModelEntity? = nil
    var pivot = SCNMatrix4MakeTranslation(0.069, 0.155, 0)
    var timer: Timer? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
             
        timer = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(addARElement), userInfo: nil, repeats: true)
        arView.scene.addAnchor(anchor)
        anchor.addChild(rootEntity)
        self.rootEntity.position = [0,0,-0.5]
    }
    
    @objc func addARElement() {
        plainCard?.removeFromParent()
        plainCard = nil

        plainCard = ModelEntity(mesh: MeshResource.generateBox(width: 0.2, height: 0.11,depth: 0), materials: [UnlitMaterial(color: .red)])
        plainCard?.transform = Transform(matrix: simd_float4x4(pivot))
        rootEntity.addChild(plainCard!)
    }
}

Here is my question:

With this creation of box at particular interval, memory continues to increase, there is significant increase in CPU usage, energy impact is high. After one point the app crashes because of excessive memory usage. What can be going wrong here? Where is the memory leak happening?

enter image description here

After 5 mins of running app: the frame dropped to 30, memory has gone up from previous image and energy impact is very high

a

I tried Xcode's instruments which shows Heap allocation is getting significant spike enter image description here

1 Answers

Your code is just fine. RealityKit has a huge memory footprint that will be left there. There is a small room for improvement tho. First if your code is in UIKit, you should add the following:

func removingView() {
    self.arView?.session.pause()            // there's no session on macOS
    self.arView?.session.delegate = nil     // there's no session on macOS
    self.arView?.scene.anchors.removeAll()
    self.arView?.removeFromSuperview()
    self.arView?.window?.resignKey()
    self.arView = nil
}


deinit { removingView() }

Second, to reduce memory footprint you can set up the ARView with the following render options:

arView.renderOptions = [
        .disableHDR,
        .disableDepthOfField,
        .disableMotionBlur,
        .disableFaceMesh,
        .disablePersonOcclusion,
        .disableCameraGrain,
        .disableAREnvironmentLighting
    ]

And that's about it. There's an ongoing thread on the Apple forum. You can also go there and give a vote to push Apple engineers to fix this bug,

Related