I am trying to create 2d platform game using SpriteKit. The game contains 21 levels, this means 21 scenes and 21 tilesets as well. Each scene contains several tilenodes, all of which uses only 1 tileset. At some point I found that my game consume almost 2GB of memory and sometimes being terminated due to memory issue.
I started investigation of reasons for this memory leaks using instruments. I found 4 leaks which shown on attached screenshot. Responsible library is JavaScriptCore, I have no idea what the hell that means.
So, I continue investigation and try to remove blocks of code and assets from project in order to understand what going on.
At the end my project contained:
Code of the GameViewController:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GKScene(fileNamed: "GameScene") {
if let sceneNode = scene.rootNode as! SKScene? {
sceneNode.scaleMode = .aspectFill
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
}
}
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override var prefersStatusBarHidden: Bool {
return true
}
}
So, no code which can cause leaks and the game still consume 2GB in peaks. And only when I remove all tilesets from project except one which is required for level - the memory consumption became normal, like ~200mb.
My guess that the SpriteKit load all tilesets into memory despite all tilenodes of the scene using only 1 tileset.
Also, game works well if I put tilesets back to the project and remove all tilenodes from scene.
My questition is how to properly manage this situation?
Thank you!
Update1: Memory Graph is attached Update2: GameViewController code is provided
I found the cause of that problem. All my tilesets are in different files, each tileset has own sks file. When i moved all tilesets into one sks file the problem is gone. I assume that at some point game try to load all exist tilesets for every sks file and takes too much memory.