Bartek Wilczynski bio photo

Bartek Wilczynski

IT consultant and entrepreneur with over 10 years of experience in a variety of medium-size line of business applications, mostly in .NET technology.

Email Twitter LinkedIn Youtube

If you haven't been playing with Obj-C on iOS yet and came from a different world, like .NET, Java or any scripting languages there is a really BAD news for you - although it is available on Mac (as of 10.5) garbage collector isn't supported on any iOS device. It's a known fact that running garbage collector takes some additional resources which are very limited on mobile devices when compared to desktop machines, that's why the decision to handle memory management via manual reference counting seems quite reasonable. Although it is possible to implement GC on a phone (Android does it!) it drains battery much more, memory consumption is bigger and application might sometimes lose its responsiveness when GC is doing its job.

I am not going to dive into low level details of memory management, as you will find a great documentation on Apple, but instead I want to show you some most common use cases and general rules of memory management on iPhone / iPod Touch and iPad devices.

  1. The most important rule is that you ONLY need to release objects that you own, meaning those ones that are allocated using "alloc". Every "alloc" should have a corresponding "release" otherwise a memory will leak.
    NSString *text = [[NSString alloc] init];
    // do something with text
    [text release];
  2. If you haven't created an object by your own using "alloc" it means that this object is an autoreleased object that will be later on released by auto release pool. You CANNOT release it as it would throw an error.
    NSString *text = [[NSString stringWithString:@"Sample text"];
    // do something with text
    // object will be automatically released later on by auto release pool
    // [text release] would throw an error here
  3. You need to retain objects that you don't own, otherwise they might be released when you still needs them. The easiest way to do this is by levaraging Objective-C properties.
    // in your .h file
    @property (retain) NSString *text;
    // in your .m file
    @synthesize text;
    // assigning auto released object to propery will retain it
    self.text = [NSString stringWithString:@"Sample text"];
  4. You need to perform a manual cleanup of all properties you retained or all variables in which you keep a reference to object you own in a dealloc method. The easiest way is to just assign nil to property - this will solve both a problem of releasing the previous value and assigning a property to nil.
    // in your .m file
    - (void)dealloc {
    	self.text = nil;
    	[super dealloc];
  5. When you add an object to a collection, this collection takes an ownership of this object so you don't need to retain it by yourself. Collection relinquishes ownership when it is released.
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSString *text = [NSString stringWithString:@"Sample text"];
    [array addObject:text]; // collection will retain a text
    [array release]; // text will be released

That's pretty much all, it takes some time to get used to it and it's not as simple as automatic memory management in garbage collector controlled environments. You need to be very careful, invalid memory handling will result in memory leaks or application crashes due to accessing deallocated objects.