2

Maybe this is easy, but I'm lost.

I have a project where I have to make a full-screen animation that uses 8 jpgs to simulate a page opening. So what I am doing is:

  • I have an SKSpriteNode that displays on the full screen
  • making an array of 8 SKTextures
  • using SKTexture preloadTextures to load them
  • once they're loaded, I use animateWithTextures to show the animation
  • later on, another method removes the SKSpriteNode from the scene.

When I first do the page turn, it uses a ton of memory, but when I run removeFromParent on it, the memory continues to be used.

My .m file declares this at the top:

SKSpriteNode *pageTurnNode;

because I want to be able to refer to it easily in both methods.

How do I get rid of all those textures and whatnot?

Cocorico
  • 2,319
  • 3
  • 22
  • 31
  • 1
    You still have an array of 8 SKTextures, correct ? Would need to see your code to be certain, but as long as that array exists there is still a reference to those textures. – prototypical Jan 09 '14 at 19:24
  • Yeah, I didn't mention it, but I did put [arrayName removeAllObjects] immediately after using it. – Cocorico Jan 10 '14 at 01:07

2 Answers2

4

Textures may not be released from memory immediately. Apparantely Sprite Kit employs a caching system. It will remove cached textures when it sees fit.

That and what @prototypical said.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I guess this is correct, but it's weird because it basically never removes the cached textures, or at least not before the app crashes (because other things get put in memory). Don't know what I can do, maybe not use ARC in this case or something. – Cocorico Jan 10 '14 at 01:08
  • Are you sure the crash is based on the memory ? You didn't mention a crash in your question. Do you keep creating a new page turn animation ? – prototypical Jan 10 '14 at 01:30
  • The crash is due to low memory, and the problem is that once I load this animation, my memory usage gets pushed RIGHT to the edge. Then, if I add a few more things that use some memory, THAT is when it crashes. – Cocorico Jan 11 '14 at 17:35
2

This seems to be pretty valuable and relates to this question, I feel.

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:

  1. create UIImages with +imageWithContentsOfFile:
  2. create SKTextures from the resulting UIImage objects by calling SKTexture/+textureWithImage:(UIImage*).

Pulled from this answer: iOS 7 Sprite Kit freeing up memory

Community
  • 1
  • 1