Memory issues - Living vs. overall -> app is killed

484 Views Asked by At

I'm trying to check my applications memory issues in Instruments. When I load the application I play some sounds and show some animations in UIImageViews. To save some memory I load the sounds only when I need it and when I stop playing it I free it from the memory.

problem 1:

My application is using about 5.5MB of Living memory. BUT The Overall section is growing after start to 20MB and then it's slowly growing (about 100kB/sec). But responsible Library is OpenAL (OAL::Buffer), dyld (_dyld_start)-I am not sure what this really is, and some other stuff like ft_mem_qrealloc, CGFontStrikeSetValue, …

problem 2:

When the overall section breaks about 30MB, application crashes (is killed). According to the facts I already read about overall memory, it means then my all allocations and deallocation is about 30MB. But I don't really see the problem. When I need some sound for example I load it to the memory and when I don't need it anymore I release it. But that means when I load 1MB sound, this operation increase overall memory usage with 2MB. Am I right? And when I load 10 sounds my app crashes just because the fact my overall is too high even living is still low??? I am very confused about it.

Could someone please help me clear it up?

(I am on iOS 5 and using ARC)

SOME CODE:

creating the sound OpenAL:

MYOpenALSound *sound = [[MyOpenALSound alloc] initWithSoundFile:filename willRepeat:NO];

if(!sound)
    return;

[soundDictionary addObject:sound];

playing:

[sound play];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, ((sound.duration * sound.pitch) + 0.1) * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[soundDictionary removeObjectForKey:[NSNumber numberWithInt:soundID]];
    });
}

creating the sound with AVAudioPlayer:

[musics replaceObjectAtIndex:ID_MUSIC_MAP withObject:[[Music alloc] initWithFilename:@"mapMusic.mp3" andWillRepeat:YES]];

pom = [musics objectAtIndex:musicID];
[pom playMusic];

and stop and free it:

[musics replaceObjectAtIndex:ID_MUSIC_MAP withObject:[NSNull null]];

AND IMAGE ANIMATIONS: I load images from big PNG file (this is realated also to my other topic : https://stackoverflow.com/questions/12223714/memory-warning-uiimageview-and-its-animations) I have few UIImageViews and by time I'm setting animation arrays to play Animations...

UIImage *source = [[UIImage alloc] initWithCGImage:[[UIImage imageNamed:@"imageSource.png"] CGImage]];

cutRect = CGRectMake(0*dimForImg.width,1*dimForImg.height,dimForImg.width,dimForImg.height);
image1 = [[UIImage alloc] initWithCGImage:CGImageCreateWithImageInRect([source CGImage], cutRect)];
cutRect = CGRectMake(1*dimForImg.width,1*dimForImg.height,dimForImg.width,dimForImg.height);
...
image12 = [[UIImage alloc] initWithCGImage:CGImageCreateWithImageInRect([source CGImage], cutRect)];

NSArray *images = [[NSArray alloc] initWithObjects:image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12, image12, image12, nil];

and this array I just use simply like :

myUIImageView.animationImages = images, ... duration -> startAnimating
2

There are 2 best solutions below

3
On

Suggestions to remove memory leaks:

1) Use iOS5 feature of ARC.

2) Further checkout the memory leaks in your project using this

Hope this helps

1
On

MYOpenALSound *sound = [[MyOpenALSound alloc] initWithSoundFile:filename willRepeat:NO];

You never release this. You have a retain count of one on allocation.

Calling replace on the array its in or setting the array index to NSNull, does not release the object.

You must call release on every instance of the sounds you are storing.