iOS 7 Sprite Kit freeing up memory

Lehel Medves picture Lehel Medves · Oct 8, 2013 · Viewed 13.5k times · Source

I am building an iOS game aimed for the new iOS 7 and Sprite Kit, using emitter nodes and physics to enhance gameplay. While developing the app, I ran into a serious problem: you create your scenes, nodes, effects, but when you are done and need to return to the main screen, how do you free up all the memory allocated by these resources?

Ideally ARC should free up everything and the application should get back to the memory consumption level it had before creating the scene, but this is not what happens.

I've added the following code, as the dealloc method of the view, which draws the scene and is responsible for removing everything upon getting closed (removed):

- (void) dealloc
{
    if (scene != nil)
    {
        [scene setPaused:YES];

        [scene removeAllActions];
        [scene removeAllChildren];

        scene = nil;

        [((SKView *)sceneView) presentScene:nil];

        sceneView = nil;
    }
}
  • sceneView is a UIView, which is the container of the scene
  • scene is an extension of the SKScene class, creating all the SKSpriteNode objects

I would very much appreciate any help on this matter.

Answer

Cocorico picture Cocorico · Feb 4, 2014

I had a lot of memory problems with Sprite Kit, and I used a tech support ticket to get info, and it may relate here. I was asking whether starting a new SKScene would totally release all the memory the previous one used. I found out this:

The underlying memory allocated by +textureWithImageNamed: may or may not (typically not) be released when switching to new SKScene. You cannot rely on that. iOS releases the memory cached by +textureWithImageNamed: or +imageNamed: when it sees fit, for instance when it detects a low-memory condition.

If you want the memory released as soon as you’re done with the textures, you must avoid using +textureWithImageNamed:/+imageNamed:. An alternative to create SKTextures is to: first create UIImages with +imageWithContentsOfFile:, and then create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).

I don't know if this helps here.