Select Page

In iOS 5 “ARC” has been introduced. ARC stands for Automatic Reference Counting. The posting below is still valid for iOS < 5. In iOS 5 the there still is reference counting, but it is automatic. Therefore ARC is no garbage collection. ----------- Platforms like dotnet and Java do memory management automagically. They are "managed" environments. Users of those environments usually don't have to care about memory management. iOS is NOT one of those environments! In iOS the programmer needs to care about memory management. That might sound difficult (especially for a dotnet/Java programmer!) but the rules for iOS memory management are simple. 1. You (the programmer) have to manage the memory when you OWN the object. (If you do not own the object, you should not care about memory management for that object).

2. When do you own an object? If you got control of the object through use of the following keywords:

  • Retain
  • Alloc
  • New
  • Copy

These keywords make the easy to remember word “R.A.N.C“. E.g. if you used on of the RANC keywords, you own the object and therefore you have to take care of memory management for that object.

3. If you own the object, you are responsible for releasing it. How do you do that? Easy, the “release” keyword. Example:

1
2
3
  NSString *myString = [[NSString alloc] initWithString:@"String created with alloc+init"];
  MyLabel.text = myString;
  [myString release]; // We used "alloc" therefore we have to release ! (RANC)

 
4. Lot’s of times you can avoid becoming owner of an object. A lot of iOS methods return an object without making you owner. Example:

1
2
3
  NSString *myString = @"String created without alloc+init";
  MyLabel.text = myString;
  //We did not use a RANC keyword, therefore we are not the owner and do not release.