-1

I'm trying to make a kind of 360° visualizer with spriteKit. In my scene i create a

spriteArray = [[NSMutableArray alloc] init];

and i'm filling it in this way:

for ( int j=0; j<stepsY+1; j++)
{
    NSMutableArray * innerArray = [[NSMutableArray alloc] init];

    for ( int i=0; i<stepsX+1; i++) {
        SKTexture *t = [SKTexture textureWithImageNamed:[NSString stringWithFormat:@"img_0_%d_%d.jpg", (stepsY-j),(stepsX-i)]];
        [innerArray addObject:t];
    }

    [spriteArray addObject:innerArray];
}

for example i will have a two dimensional array 4x23; with four steps for y axis and 23 step for x axis; on

- (void) touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event

i'm using

[sprite setTexture:....];

to set the right texture to visualize;

When i'm running it on the simulator it works perfectly but when i try on the real device, when i'm filling the spriteArray the app crashes and xcode is telling me "Terminated due to Memory Error"; So i checked the use of ram in simulator and i recognize that is huge... 1.5gb; my texture are 1220x1220 jpg each one around 50kb.... so it's seems that there is a kind of memory leak somewhere.

Can somebody help me please?

Thanks

1 Answers1

1

Not a memory leak, just a misconception: file size has nothing to do with texture memory usage.

1220x1220 amounts to 1,488,400 pixels. By default textures use 32 bits per color (4 bytes). This gives you about 5.7 MB memory usage per texture.

4 times 23 is 92.

92 times 5.7 MB gives you a memory usage of well over 500 Megabytes.

Furhtermore, if Sprite Kit internally uses only textures with dimensions that are multiples of 2 then the real texture size would be 2048x2048 (16 MB per texture) and a total texture memory usage of nearly exactly 1.5 Gigabytes.

Well, there's your problem. :)

CodeSmile
  • 64,284
  • 20
  • 132
  • 217