Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Possible memory leak while creating mesh

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

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!