创建网格时可能存在内存泄漏

发布于 2025-01-14 20:31:42 字数 1910 浏览 0 评论 0原文

我正在使用 RealityKit 在特定时间间隔后生成网格并将其作为子节点添加到根节点。在创建另一个网格之前,我将从父节点中删除先前创建的实体,并为其分配 plainCard = nil

下面是示例代码:

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!)
    }
}

这是我的问题:

随着以特定间隔创建盒子,内存持续增加,CPU 使用率显着增加,能源影响是高的。一段时间后,应用程序会因内存使用过多而崩溃。这里可能出了什么问题?内存泄漏发生在哪里?

输入图片此处描述

运行应用 5 分钟后:帧数下降到 30,内存比之前的图像有所增加,并且能量影响非常高

a< /a>

我尝试了 Xcode 的工具,显示堆分配正在显着增加 输入图片此处描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2025-01-21 20:31:42

你的代码很好。
RealityKit 会留下巨大的内存占用。
不过还有一个小的改进空间。
首先,如果您的代码位于 UIKit 中,则应添加以下内容:

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() }

其次,为了减少内存占用,您可以使用以下渲染选项设置 ARView:

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

就是这样。 Apple 论坛上有一个正在进行的主题。你也可以去那里投票推动苹果工程师修复这个bug,

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文