Memory leak: SpriteKit + iPhone 6s + iOS 10

Hi


I have a memory issue in my SpriteKit game. Memory increases when any of the sprites is moving on the screen (memory increase stops if i stop all motions)

I noticed this running the game on my iPhone 6s with iOS 10. I tested it on an iPhone 5s running iOS 9.3.5, and it was fine, there was no memory increasing during movements.


An easy way to reproduce:


1. Create new project in Xcode 8, new Game, Language: Swift, Game technology: SpriteKit, Devices: iPhone

2. Add the following line at the end of didMove(to view..:

label?.run(SKAction.rotate(byAngle: 50, duration: 100))

3. Run the project on iPhone 6s device, running iOS 10 (I'm not sure which one is causing the bug, iOS10 or the phone..)

Memory allocation increases (at about 0.1MB/10s speed) until the rotation ends.

Using "Leaks" instument I noticed that "CAMetalDrawable" and "__NSArrayM" categories are increasing.

I'm confused because I invested a lot of time in my SpriteKit game, and now I'm not able to debug this leak, I'm afraid it's caused by the framework.


Thanks for the help!

I'm having a similar issue and unfortunately haven't figured out how to fix it. My game is complete and built on iOS 9 and SpriteKit. Running it leads to no memory increase and no leaks (instruments) - iPad 4. On my iPhone 6 on iOS 10 I get hundreds of leaks every 20 or so seconds and my memory increases. Before upgrading the phone from 9.3.5 I ran instruments on it and had no leaks. Most are in foundation and core foundation. Seems like an iOS 10 bug...

I have the same issue. If there was a way to get rid of the persistent data then I could deal with it. But this is immpossible to deal with.

I have the exact same issue on tvOS 10. I had no problems running my app on tvOS 9. It is becoming clear to me that this is an Apple bug. I have been digging, researching, experimenting, and sobbing trying to resolve this. "[CAMetalDrawable initWithDrawablePrivate:layer:]" inside "_NSArrayM" is the likely culprit. I bookmarked this thread. If I find a fix, I'll post it here.

I've spent a lot of time trying to find out what is causing this and I have not been able to find the root cause so I also believe that it is an Apple bug. However, I've found some useful tips. If you want to stop the increasing memory usage you need to call SKView.isPaused = true when you are not using the SKScene and the memory will stop increasing. Then call SKView.isPaused = false when you wish to resume activity on your SKView. Additionally, the only way I've been able to release all the useless memory and kill those persistent#, is by adding the SKView as a subview of the any UIView you wish and when you are done with a cycle of your game you need to removeFromParent the SKView and set it = to nil, then assign a new empty SKScene to the SKView and removing it again and setting it to nil again. Finally, to relaunch the game for a second cycle, prepare and load the SKScene and SKView as if it were the first cycle (So reuse your inial game loading method). It's quite tedious but it works and makes your app feel and look like it is functioning correctly according to insruments. Well this fix requires that your game is not 100% based upon a SKView as you will have to kill it... Good luck!

Thanks for the info! Unfortunately, my app is built entirely on adding and removing SKNodes within one scene. I ran a test using the tips you mentioned, and I did notice that the memory is cleaned up nicely. It's strange that we have to jump through all of these hoops to achieve something that simply worked in previous versions. I'm in the process of evaluating if I should re-architect my app to be able to use this "fix" 😝

Accepted Answer

Hello Everyone,


I've experienced this problem even with a single node in a (SpriteKit) Scene which is doing absolutely nothing.(Fixed in Place)


Try dropping down to OpenGL by setting 'PrefersOpenGL' to 'YES' in Info.plst. (Thank Bob from DT Support!)


The only tradeoff seems to be slightly higher CPU usage, but the leaking stopped in my case with no other changes.


(I'm using a single View at the moment but may have to incorporate SubViews to fix a different issue (Memory Leak when switching scenes).


Using the following:


OS Sierra

iOS 10.1


Objective-C


Bug Report:

28853557


Update:


Still Occuring on iOS 10.1.1

I tried this, and it works! The leakage has stopped! I ran my app for 3 days, and the memory stayed at a consistent level, whereas before the memory usage would increase ~60mb every hour.


Cheers WareChameleon!

Thanks for confirming!


Hopefully the bug will be addressed soon and we can get the CPU savings back.

It seems that the issue has been addressed in the 10.2 Beta 14C5069c.


Memory usage is stable with no code changes.


Metal Thread:


https://forums.developer.apple.com/thread/67384


I'm going to test the watchOS Beta as well soon because I know my project was leaking memory and dropping frames after running for a significant amount of time.

In that case I'm going to wait for that version to release. The extra CPU usage in openGL for me goes from 78% max on Metal to 120% Max on openGL.

Thank you so much! 🙂

No Problem!


Yet another update:


In my case the leak was very slow so I turned Metal back on for the iPhone/iPad/TV in anticipation of iOS 10.2


The watchkit version of my game is a different story though, after leaving the app and coming back, the game experiences nasty frame rate drops, so I would advise to test very carefully if you're supporting an Apple Watch Version.

Maybe Same issue. Open case since October 2016, support tech is trying his best since then. When running instruments and looking at allocations, stuck in the physics contact processes. Sub-classed all the copy, dealloc, and skspritenodes to count all allocs and de-allocs. these numbers work out to be perfect but the app 'hangs with 100% cpu and runs to 2GB of memory+ before getting killed, it is stuck in physics processes according to my instruments traces.

This issue is was patched in iOS 10.2. What are you testing you app with?

Memory leak: SpriteKit + iPhone 6s + iOS 10
 
 
Q