emrekyv
emrekyv

Reputation: 1266

High memory usage in SpriteKit when using texture atlas

I have 100 textures, each sized ~100 kb, and I want to create a SKAction with animating these textures for 4 seconds with 25fps, and looping the action forever. I'm loading them from an atlas (or several, as Xcode warns me that it splits the atlas into 9).

The problem is the memory usage increases from 4 to 150 mb's when I run the action. Everything's normal in the first three steps. Total size of the assets is ~12 mb.

Can the multi-atlas usage (Xcode splitting them on compile time) can cause this problem?

// Load the frames to an array
NSArray *sTreesRightAnimationFrames = ITBLoadFramesFromAtlas(@"s01-trees-right", @"s01-trees-right-", 100);
// Create the action
SKAction *sTreesRightAction = [SKAction animateWithTextures:sTreesRightAnimationFrames timePerFrame:1.0f / 25.0f];
// Create the forever action
SKAction *sTreesRightForeverAction = [SKAction repeatActionForever:sTreesRightAction];

// Preload the textures and run the action
[SKTexture preloadTextures:sTreesLeftAnimationFrames withCompletionHandler:^{
    [treesLeft runAction:sTreesLeftForeverAction];
}];

Upvotes: 0

Views: 955

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

Sizes are filesizes, I presume? Because they don't tell you how much memory the texture from a given image will use. To calculate that for any given (atlas) image, use:

width x height x (color bit depth / 8) = size in bytes

For example:

1024 x 1024 x (32 / 8) = 4194304 Bytes = 4 Megabytes

Upvotes: 3

Related Questions