4

I initialise about 30 animated textures in my view controller on game load like this:

for i=0; i<atlasName.count; i++ {
        frameNumber = animationTextures.animationTextureAtlas[i].textureNames.count
        firstFrameName = "\(atlasName[i])1"

        animationTextures.animationFrames.append([animationTextures.animationTextureAtlas[i].textureNamed(firstFrameName)])

        for j=2; j<=frameNumber; j++ {
            var frameName = "\(atlasName[i])\(j)"
            animationTextures.animationFrames[i].append(animationTextures.animationTextureAtlas[i].textureNamed(frameName))
        }

        animationTextures.firstFrame.append(animationTextures.animationFrames[i][0])
    }

Basically I put all of the animations into an array so I can access them easily.

During the game each of these animations appear for a short amount of time and when I don't need them I use the .removeFromParent() to delete it, but the problem is that it still stays in the memory of the device and therefor once about 15-20 animations appear in the game and go into the memory it gets to about 450-500MB and crashes the game on iPhone 4 and 4s.

Does anyone know how I can really remove that animation from memory so that it doesn't take up space once it's not needed anymore?

Mario Plantosar
  • 804
  • 9
  • 24
  • I can tell you for a fact that SpriteKit has an automated memory management system built-in that is meant for situations like these. It will deallocate automatically if texture data take up too much space. I don't think its the textures themselves necessarily (unless you have ALOT), and the crash probably could be related to what you are doing to those textures. – MaxKargin Apr 02 '15 at 22:35
  • 1
    Here are some useful posts about this : http://stackoverflow.com/a/21541819/3402095 , http://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget – Whirlwind Apr 03 '15 at 00:05
  • It almost sounds like it is a combination of things. First being perhaps you aren't reusing the textures even though you are putting them into an array. Second being you still have a reference to the sprite even though you call removeFromParent(). Also you might want to look at the array you created. It is hard to follow when you have vars named frameNumber when they are actually counts and such. Your array may be much larger than expected. Hopefully that helps. – Skyler Lauren Apr 03 '15 at 04:19
  • Thank you all for the answers, I found out that the problem is the 2 animations I have running in loop on the menu screen, and not the ones in the actual game. – Mario Plantosar Apr 03 '15 at 09:19

1 Answers1

4

SpriteKit utilizes its own caching mechanism and there is no way for you to clear out the cache yourself.

From what you said, it appears that you have a memory leak somewhere in your code. Keep in mind that you need to remove all references for a texture to get deallocated by ARC.

Removing a texture from parent is not enough in your case as the textures are stored in an array. You would need to remove all textures from the array and have no other references to them in order to properly release them.

450MB+ is absolutely too much for just a couple of textures. I suggest you run Instruments and check for memory leaks.

sangony
  • 11,636
  • 4
  • 39
  • 55
  • Thank you for your answer, I knew the things you say about ARC and removing all of the references. After searching for memory leaks and allocation I found out that the problem is actually the 2 animations running in a loop on the menu screen. Each time the loop starts it allocates a bunch of memory and it stops when it gets to about 180MB. – Mario Plantosar Apr 03 '15 at 09:23