I have many (if not all) articles about SO and other disaster sites related to SpriteKit and memory issues. My problem, like many others, is that after I left the scene with SpriteKit, you free some of the memory added during the scene session. I tried to implement all the proposed solutions in the articles you found, including, but not limited to ...
1) Confirm that the method deinitis being called in the SKScene class.
2) Confirm the strongreferences to the parent VC in the scene class.
3) Strongly remove all children and activities and set the scene to nilwhen the VC disappears. (Setting the scene to nilwas what the method received deinitin order to get the call as a result)
However, after all this, memory still exists. In a way, this application goes between the standard UIKit view controllers and the SpriteKit scene (this is a professional drawing application). As an example, the application uses about 400 MB before entering the SpriteKit scene. After entering the scene and creating several nodes, the amount of memory increases to 1 GB (everything is still good). When I leave the scene, memory drops about 100 MB. And if I enter the scene again, he continues to pile up. Are there any ways or suggestions on how to completely free up all the memory that was used during a SpriteKit session? The following are some of the methods that are used to resolve this problem.
SKScene Class
func cleanScene() {
if let s = self.view?.scene {
NotificationCenter.default.removeObserver(self)
self.children
.forEach {
$0.removeAllActions()
$0.removeAllChildren()
$0.removeFromParent()
}
s.removeAllActions()
s.removeAllChildren()
s.removeFromParent()
}
}
override func willMove(from view: SKView) {
cleanScene()
self.removeAllActions()
self.removeAllChildren()
}
VC Performance
var scene: DrawingScene?
override func viewDidLoad(){
let skView = self.view as! SKView
skView.ignoresSiblingOrder = true
scene = DrawingScene(size: skView.frame.size)
scene?.scaleMode = .aspectFill
scene?.backgroundColor = UIColor.white
drawingNameLabel.text = self.currentDrawing?.name!
scene?.currentDrawing = self.currentDrawing!
scene?.drawingViewManager = self
skView.presentScene(scene)
}
override func viewDidDisappear(_ animated: Bool) {
if let view = self.view as? SKView{
self.scene = nil
view.presentScene(nil)
}
}