1

The memory of app increased after segue to VC with scnView. I've used deinit and set geometry to nil but it didn't help. I saw some tips on stack about using deinit to solve this issue, but it doesn't work for me. Memory increased every time when I back to this ViewController with scnScene SceneKit: too much memory persisting

var ship1: SCNNode!
    var ship2: SCNNode!
    var ship3: SCNNode!
    var ship4: SCNNode!
    let cameraNode = SCNNode()
    let lightNode = SCNNode()
    let ambientLightNode = SCNNode()

 class RecordVideoViewControllerWS: UIViewController {


    @IBOutlet weak var scnView: SCNView!

     override func viewDidLoad() {
            super.viewDidLoad()
          cameraNode.camera = SCNCamera()
                scnScene.rootNode.addChildNode(cameraNode)
                cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
                scnView.scene = scnScene

                lightNode.light = SCNLight()
                lightNode.light!.type = .omni
                lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
                scnScene.rootNode.addChildNode(lightNode)

                // create and add an ambient light to the scene
                ambientLightNode.light = SCNLight()
                ambientLightNode.light!.type = .ambient
                ambientLightNode.light!.color = UIColor.darkGray
                scnScene.rootNode.addChildNode(ambientLightNode)
                 ship1 = nodeFromResource(assetName: "shipFolder/test0", extensionName: "scn")
                ship2 = nodeFromResource(assetName: "shipFolder/test1", extensionName: "scn")
                ship3 = nodeFromResource(assetName: "shipFolder/test2", extensionName: "scn")
                ship4 = nodeFromResource(assetName: "shipFolder/test3", extensionName: "scn")


                scnScene.rootNode.addChildNode(ship1)
                scnScene.rootNode.addChildNode(ship2)
                scnScene.rootNode.addChildNode(ship3)
                scnScene.rootNode.addChildNode(ship4)
    }



    override func viewWillDisappear(_ animated: Bool) {

                ship1.removeFromParentNode()
                ship1.geometry = nil

                ship2.removeFromParentNode()

                ship2.geometry = nil

                ship3.removeFromParentNode()

                ship3.geometry = nil

                ship4.removeFromParentNode()

                ship4.geometry = nil

                cameraNode.removeFromParentNode()
                cameraNode.geometry = nil
                lightNode.removeFromParentNode()
                lightNode.geometry = nil

                ambientLightNode.removeFromParentNode()
                ambientLightNode.geometry = nil



                }
 deinit {
        scnScene.rootNode.cleanup()
    }


}

    extension SCNNode {
        func cleanup() {
            for child in childNodes {
                child.cleanup()
            }
            geometry = nil
        }
    }
Diana
  • 683
  • 1
  • 8
  • 17
  • A couple of questions: 1. Are you sure that your `deinit` is being called (i.e. that you don’t have a strong reference cycle preventing the scene from being deallocated at all)? 2. You say “I saw some tips on stack about ...” ... please include those references if possible. 3. What is leaking and how memory are we talking about? What does “debug memory graph” tell you? What does Instruments’ Allocations tool tell you? We need some details about the type and size of the leak. This is very unclear. – Rob Jul 06 '19 at 21:12
  • Unrelated, but in `viewWillDisappear`, remember to call `super` rendition. – Rob Jul 06 '19 at 21:13
  • Every time when I back to `VC` with `SCNView`, memory increases , on Instruments Allocations it shows that `CG raster data` increased on `~18mb-20 mb`, each `CoreGraphics CGDataProviderCreateWithCopyOfData` is `6,94 mib`. I've add super, jus forgot to copy to post, sorry – Diana Jul 06 '19 at 21:19
  • First, it’s the “persistent” number that’s going up in instruments, right, not “total”, for example? Second, is the view controller being deallocated called? (I.e. are you seeing deinit called and/or you’re not seeing objects that should be released in “Debug Memory Graph”?) Third, can you share the links that you reference? – Rob Jul 06 '19 at 21:38
  • @Rob 1)yes it's “persistent”, the total is much bigger 2)I'm not sure about `deinit`, I'm using it like I showed above , I used this post as reference https://stackoverflow.com/questions/35687122/scenekit-too-much-memory-persisting. Instruments’ Allocations shows increasing of `VM: CG raster data` and `VM: IOSurface` – Diana Jul 06 '19 at 21:44
  • @Rob I've a little bit edited my post to be more clear, if I should provide some more details please let me know. I stuck on it and don't know but something's wrong here. I tried to use local variables (instead of putting them outside of a class, but the issue still appears) – Diana Jul 06 '19 at 21:53
  • 1
    In terms of “total” v “persistent”, the “total” in Instruments doesn’t matter (because that includes memory that’s already freed). The only thing that matters is the “Persistent” column. And it doesn’t matter how much it goes up the first time, but rather how much it continues to go up each subsequent time. A snapshot of the allocations graph (like I did in that repo) would be useful. In terms of `deinit`, add a breakpoint and/or log statement and confirm that’s being reached. If not, you’re wasting your time looking further. It’s always the first thing to confirm. – Rob Jul 07 '19 at 17:42

0 Answers0