Some tips about Memory managament in Objective C

Despite the fact that I started programming in Pascal  at the age of 12, I have to admit Objective C is not something simple that I can learn and produce something useful in a few weeks. Why is it not simple? I think the main reason for me is dealing with memory.
Memory handling is, maybe, easy at first, but when you start writing something a little more complicated, you will start to have a good amount of sleepless nights debugging errors. Because when memory is not being handled properly, your program will be very unstable, meaning it will crash at very random point and it is very hard to debug.
Honestly, I still don’t find myself very confident about this topic yet. However, after 1 year learning Objective C and the 4 months developing Manga Rock, I did learn a thing or two that I think it’s worth sharing. Hopefully, these below tips will help you a bit when dealing with memory management in Objective C.


1. Memory debugging with NSZombie and Instruments

Using NSZombie and Instruments to debug is helpful when you are access an object with an reference count equals to 0. So check out this link and learn how to use it so when your program gives an error like “Bad Access”, you will know how to debug.

However, when you are still inexperienced with Objective C, and you are so afraid of memory leaks like me when I first started, you will tend to over release an object. If your program ever crashes without giving any kind of error, trust me, the first thing you should do is to find out if you are over releasing any object.

2. Collection classes and memory management

When you access object in a collection  like NSArray or NSMutableArray, it’s better to retain it and release it once you are done with it. Like the example below:

1
2
3
4
5
6
NSArray *pages = [data sortedArrayUsingDescriptors:sortDescriptors];
for (int i = 0; i < [pages count]; i++) {
     Page *p = [[pages objectAtIndex:i] retain];
     [self addToOperationQueueWithPage:[pages objectAtIndex:i] atPosition:i];
     [p release];
}

Why you need to do this? If your program is not using multi-threading, it’s fine to not follow this. But if your program does use any kind of multi-threading like NSThread or NSOperation, I strongly recommend you to follow the above pattern. Because if you don’t retain the object you are accessing, your program may crash when you are trying to access any object of the array and at the same time, other thread has just release that array object.

3. Nil object

This link from the iPhone OS Development Blog is a good link to explain the relationship between Nil object and memory handling. I do find that assigning the object to nil after releasing it does help my application, Manga Rock much more stable. And I think the reason is that in Manga Rock, I use threading a lot to make the application more responsive, so by assigning the object to nil, I will be sure that the object get deallocated.

4. Object Copying

If you are dealing with any mutable object like NSMutableString, it’s better to make a copy of the object before using it for anything else. This will help you to prevent the value of object unexpectedly (like by other thread).

Anyway, for now I can remember only these above tips about memory management that really helped me to make Manga Rock a solid application. So if I get to learn more about memory management in Objective C, I will definitely create another post and share with you all. And if you know anything useful about dealing with memory management in Objective C, please share with me by commenting on this post .

One last note for people who just started learning Objective C,  picking up a good book like Programming in Objective-C and reading through the memory management chapter of the book is a good start. After that you should read through Apple document about memory management. Doing those readings will definitely help to save you from a lot of sleepless nights. Don’t be like me just jump straight to coding without any proper learning :).

Comments

comments