0

I'm using SKTextures

var texture1 = SKTexture()
var texture2 = SKTexture()

and SKActions

SKAction.animate(with:[texture1, texture2])

for animate SKSpriteNodes in my game.

But when animation is done, memory contains textures. How can I remove all textures from memory after animation is done? When I come to another SKScene memory are free, but I need free memory before transition. Sorry for my English Im using SWIFT 3

Nexus S.
  • 135
  • 9
  • How do you know the textures are still in memory? if anything, it is possible for texture2 to be in memory depending on settings you select to allow it to reset the texture when finished or not. – Knight0fDragon Oct 03 '16 at 06:03
  • is var texture1 global? You may have to do var texture1 = nil, but you would need to make them optional for that – Knight0fDragon Oct 03 '16 at 06:04
  • @Knight0fDragon Doing var texture1 = nil don't solve the problem: the texture is resident on the cache system. – Alessandro Ornano Oct 03 '16 at 08:28
  • @AlessandroOrnano people keep talking about this "cache system" but I do not believe it is a cache in the way people are believing it is. I am going to have to experiment when I can into this manner. I would figure it is a weak reference holding onto an SKTexture, but we know that is not the case because of a different discussion we had when imageNamed was bugged, and produced more objects. I have had many of chats with apple tier 1 engineers, and let me tell you, they are very quick to hit that resolved nothing is wrong button, so due to experience I can't believe what they say either – Knight0fDragon Oct 03 '16 at 11:45
  • @Knight0fDragon Look my answer: it's the doc that explain the SKTexture behavior's. It stay resident in memory (cache system probably..it's not specified but it could be that) and you do not have to worry about managing SKTexture because it's the task of apple. Your task is to follow tips reported to "Managing Texture Memory" chapter (avoid loading too many textures in a single pass..). We have not invented words, are terms derived from the manual.I would like to tell you is true, memory management would truly better, unfortunately the reality is different. – Alessandro Ornano Oct 03 '16 at 12:19
  • @AlessandroOrnano you are reading too much into that, that does not talk about any kind of caching system, that is actually backing up what I am saying, if all of the strong references are killed, that SKTexture should die. The rendering portion has to do with transferring texture data to VRAM. Now if you are telling me that setting a texture to nil does not remove it from memory, but removing the node does, then that right there is a flag that it is a bug with SKSpriteNode, and that bug I can actually explain. The animation action can retain the original texture before animating – Knight0fDragon Oct 03 '16 at 13:04
  • and in this process, may be why an SKTexture is being held when the developer has no strong references to it. – Knight0fDragon Oct 03 '16 at 13:05
  • About "texture to nil" I've only said : the texture is resident on the cache system. You are attributing the memory management for SKTexture to the canonical method of memory administration adopted for the other nodes: who says that Apple has not introduced a different data rescue system? It does not allow you to remove SKTexture's, there aren't sources, don't you find this should make you think about it? Believe me, I would agree with you but at present we can only reason about on. – Alessandro Ornano Oct 03 '16 at 13:21
  • Otherwise, since this topic is very interesting, you could shows that you can remove SKTexture without any problem, and I will post your results in all over StackOverflow under related topics (I'm sorry for my english error..) – Alessandro Ornano Oct 03 '16 at 13:24
  • Wrote the sample, it is in the chat I sent you an invite to @AlessandroOrnano I was right, removing all strong references from the developers side of things will remove it from the cache and memory as well – Knight0fDragon Oct 03 '16 at 19:11
  • @Knight0fDragon Very good, here in Italy it's too late (night) so tomorrow I'll try all tests, thank you. – Alessandro Ornano Oct 03 '16 at 19:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124848/discussion-between-alessandro-ornano-and-knight0fdragon). – Alessandro Ornano Oct 04 '16 at 06:37
  • @Knight0fDragon Hello man :( , I don't receive any invite, can you send my another one? – Alessandro Ornano Oct 04 '16 at 06:39
  • @Knight0fDragon No invite until now, how can I see your code? – Alessandro Ornano Oct 04 '16 at 16:52
  • weird, look for the SpriteKit chatroom on here – Knight0fDragon Oct 04 '16 at 16:52
  • @AlessandroOrnano http://chat.stackoverflow.com/rooms/124357/sprite-kit – Knight0fDragon Oct 04 '16 at 16:59
  • @Knight0fDragon This chat don't speak about texture, are you sure this is the correct link? – Alessandro Ornano Oct 05 '16 at 07:01
  • @AlessandroOrnano no idea if you got notified, check again I reposted it – Knight0fDragon Oct 05 '16 at 07:08
  • @Knight0fDragon Ok test ended, now I'll update my answer. – Alessandro Ornano Oct 05 '16 at 09:52
  • @Knight0fDragon Ok do it. Please write an answer with your code. – Alessandro Ornano Oct 05 '16 at 10:00
  • Hi guys! I'm not found the solve of my problem( Memory contains textures everytime. But I can free memory from SKTexture when I have transition to another SKScene with remove SKSpriteNode who have animation. somenode.removeFromParent() sorry for my english – Nexus S. Oct 06 '16 at 21:07
  • @Knight0fDragon I invite you in chat http://chat.stackoverflow.com/rooms/125234/opinions-from-sktexture-and-others – Alessandro Ornano Oct 08 '16 at 13:19

1 Answers1

0

Textures may not be released from memory due to caching system.

You could use the completion when you run your action for example:

self.run(SKAction.animate(with:[texture1, texture2]), completion: {
   //animation is ended, do other stuff
})

Check this answer for more detail.

From the official docs:

The texture data is prepared for rendering when:

A sprite or particle that uses the texture is part of a node tree that is being rendered.

Once the SKTexture object is ready for rendering, it stays ready until all strong references to the texture object are removed. ...

None of these textures must be loaded because their data is already in memory when the texture is created. However, the texture must still be prepared by SpriteKit before it can be used for rendering.

There are no options (for now - Swift 3) to directly deallocated these texture from memory. Anyway remain just nil assignment.

Community
  • 1
  • 1
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133