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?
After 5 mins of running app: the frame dropped to 30, memory has gone up from previous image and energy impact is very high
I tried Xcode's instruments which shows Heap allocation is getting significant spike
