创建网格时可能存在内存泄漏
我正在使用 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,内存比之前的图像有所增加,并且能量影响非常高
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码很好。
RealityKit 会留下巨大的内存占用。
不过还有一个小的改进空间。
首先,如果您的代码位于 UIKit 中,则应添加以下内容:
其次,为了减少内存占用,您可以使用以下渲染选项设置 ARView:
就是这样。 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:
Second, to reduce memory footprint you can set up the ARView with the following render options:
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,