Our app (iOS 9.1, Xcode 7.1) uses SpriteKit and is crashing with a "Terminated due to memory issue." I started removing more and more code and finally discovered that the simple act of creating and discarding plain SKScene instances will exhaust memory. In fact, something as simple as:
while true {
_ = SKScene(size: CGSize(width: 100.0, height: 100.0))
}
will eventually cause the app to crash with the same memory issue. I watch it in the debug navigator, and the "Memory" value rises quickly and then the app crashes. Using a different class, such as:
while true {
_ = [Int](count:1000, repeatedValue:0)
}
will chew up a lot of CPU, but the instances are released cleanly and memory is never exhausted.
I tried subclassing SKScene and adding a deinit that printed a message. The message was printed just as I would expected, but the memory ran out anyway.
The problem for us: we subclass SKScene with a lot of extra stuff, and the app runs out of memory if we create and discard more the 10 or 12 instances (sequentially) over the execution life of the app. There is never a time when more than two instances should be alive. We've reviewed the code and there are no strong reference cycles.
Does SKScene do anything that requires us to do something special to avoid memory issues of this type?
activeScene
, and just rerun the constructor on it every time you want a new scene? This may solve the memory issue. If you also need to run transitions, maybe have atransitionScene
global variable as well? Kinda a weird workaround, but may work. – Gliderman