Reputation: 171
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?
Upvotes: 2
Views: 183
Reputation: 59536
You are right, with this code the used memory does grow up.
while true {
_ = SKScene(size: CGSize(width: 100.0, height: 100.0))
}
But I don't think it is something directly related to SKScene
.
Infact the same memory behaviour does happen with other types (classes or structs) like UIButton
or big arrays of strings.
Here the used memory does grow up.
while true {
_ = UIButton(type: .DetailDisclosure)
}
Here the used memory does grow up as well.
while true {
_ = [String](count:100000000000, repeatedValue:"Hello world")
}
I noted that waiting a second at the end of each while iteration does solve the problem.
while true {
_ = UIButton(type: .DetailDisclosure)
sleep(1)
}
My idea is that the used memory does grow up simply because the system has not enough time to free the memory for the old objects/values.
Upvotes: 1