0

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
Community
  • 1
  • 1
D33
  • 239
  • 1
  • 3
  • 14

2 Answers2

1

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

Community
  • 1
  • 1
Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • 1, yes. this project is already using ARC. 2, thanks. It helped me to find some issues with CGImageRef and its CGImageRelease. But it didn't help me with discovering growing overall memory or if the problem is with the overall or everywhere else... :( – D33 Sep 01 '12 at 12:15
  • the **Analyze** in Xcode helps you point out all the code-related memory leaks, and not only CGImageRelease and CGImageRef – Vimal Venugopalan Sep 01 '12 at 14:20
  • Yes. I know. Well actually it showed me only the CGImageRef issues and few never used variables and so on. Not a big problems with my code. Is there any option I should enable or something? I just went into Product->Analyze and looked at blue issues in the left panel (or blue highlighted lines in my code). And fixed them all. But still my "overall memory" is still increasing. I think it is due to my UIImageView animations - I'm switching UIImageView animation arrays. Is there any +- number of "save" image animation frame count (or size in memory)? – D33 Sep 01 '12 at 14:38
0

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.

deleted_user
  • 3,817
  • 1
  • 18
  • 27
  • but I'm using ARC on this project -> so if you mean use [sound release], I think it is not allowed under ARC. Is it correct or you are talking about anything else? – D33 Sep 01 '12 at 12:14