SceneKit: too much memory persisting

后端 未结 2 1990
悲哀的现实
悲哀的现实 2021-01-07 11:33

I’m out of ideas here, SceneKit is piling on the memory and I’m only getting started. I’m displaying SNCNodes which are stored in arrays so I can separate compo

2条回答
  •  佛祖请我去吃肉
    2021-01-07 11:55

    I did also experience a lot of memory bloat from SceneKit in my app, with similar memory chunks as you in Instruments (C3DGenericSourceCreateDeserializedDataWithAccessors, C3DMeshSourceCreateMutable, etc). I found that setting the geometry property to nil on the SCNNode objects before letting Swift deinitialize them solved it.

    In your case, in you cleanup function, do something like:

    atomsNode_1.removeFromParentNode()
    atomsNode_1.geometry = nil
    atomsNode_2.removeFromParentNode()
    atomsNode_2.geometry = nil
    

    Another example of how you may implement the cleaning:

    class ViewController: UIViewController {
        @IBOutlet weak var sceneView: SCNView!
        var scene: SCNScene!
    
        // ...
    
        override func viewDidLoad() {
            super.viewDidLoad()
            scene = SCNScene()
            sceneView.scene = scene
    
            // ...
        }
    
        deinit {
            scene.rootNode.cleanup()
        }
    
        // ...
    }
    
    extension SCNNode {
        func cleanup() {
            for child in childNodes {
                child.cleanup()
            }
            geometry = nil
        }
    }
    

    If that doesn't work, you may have better success by setting its texture to nil, as reported on scene kit memory management using swift.

提交回复
热议问题