LearniOS Memory Management: The Manual-Retain-Release Model


writes on November 14, 2013

Being a Business teacher at Treehouse, where our mission is to teach people to code, it’s lame to be someone who doesn’t know how to actually code. About a year ago, I decided it was about time and set about to teach myself. I wanted to use Treehouse videos to do this because not only was it a great resource I already had, but I also got to test our curriculum and provide feedback to the relevant teachers if I found any holes. I picked iOS development because I’m pretty much an Apple fanboy and because I’ve always wanted to make apps. In this first post, I’m going to outline some of the stuff I’ve learned so far. Be gentle, the training wheels are still on. 🙂

When I tried my hand at learning iOS development a few years ago, having no prior programming experience whatsoever, one of my biggest mental blocks was in regards to iOS memory management. Retaining, releasing and deallocating objects made no sense to me and I soon gave up.

Since then ARC, or Automatic Reference Counting, has been introduced and has made the job much easier. But learning with ARC has been somewhat of a black box. Why am I assigning an object a property of strong? What’s the difference between atomic and nonatomic? Before we go into what ARC is and how it made learning easier, let’s understand why we need it in the first place.

Memory Management

Devices have a limited amount of memory and in order for apps to function efficiently, we need to distribute ownership of this limited memory among our app data and code. The concept of memory management is “the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it”. There are two ways you can accomplish this:

  • The Manual-Retain-Release method, where you explicitly manage memory by keeping track of the objects you own using a method called reference counting
  • Using ARC, or Automatic Reference Counting, which still uses reference counting, but does it automatically by inserting the appropriate memory management method calls at compile-time

In this post, the first of several posts, we are going to take a look at the Manual-Retain-Release model and how memory used to be managed so that we have an understanding of what ARC is doing in the background.


Before the introduction of ARC, everyone followed a ‘manual-retain-release’, or MRR, model of memory management. The basic rules are as follows:

  • You own any object you create
  • You can take ownership of an object using retain
  • When you no longer need it, you must relinquish ownership of an object you own.
  • You must not relinquish ownership of an object you do not own

Under these rules, if an object is created (using a method whose name beings with “alloc”, “new”, “copy”, or “mutableCopy”), it must be relinquished when it is no longer needed. To do this you send the object a release or autorelease message. Let’s look at an example:

   Car *aCar = [[Car alloc] init];
   // ...
   NSString *model = aCar.model;
   // ...
   [aCar release]

Once we’re done with the Car object, we relinquish ownership by sending a release or autorelease message. As such, relinquishing ownership of an object is typically referred to as “releasing” the object. In our example, we don’t take ownership of the string pointing to the Car model, so we don’t bother with sending it a release message.

Let’s look at another example of a method.

- (NSString *)model
   NSString *string = [[[NSString alloc] initWithFormat:@"%@ %@",                        self.carMaker, self.modelName] autorelease];
   return string;

Since we use the ‘alloc’ method to create the object, according to Rule #1, we own the string returned by alloc. It is our responsibility now, to relinquish ownership of this object before we lose reference to it. In this case, if were to relinquish it using ‘release’, the string will be deallocated before it is returned and the method would return an invalid object.

Finally, you can relinquish ownership of your objects by implementing the dealloc method. The NSObject class defines a dealloc method that is implemented automatically when an object has no owners. From the docs: “The role of the dealloc method is to free the object’s own memory, and to dispose of any resources it holds, including ownership of any object instance variables.”

In the following example, we create the instance variables carMaker and modelName. To dispose of them when we are done and free up memory, we release them in our dealloc method.

 @interface Car : NSObject
 @property (retain) NSString *carMaker;
 @property (retain) NSString *modelName;
@implementation Person
 // ...
 - (void)dealloc
   [_carMaker release];
   [_modelName release];
   [super dealloc];

Using MRR techniques

If any of your classes have properties that are objects, you have to make sure that when an object is set its value it is not deallocated while in use. To do this you have to claim ownership of the object and relinquish or release it when you are done with it. Take the following code:

@property (nonatomic, retain) NSString *string;

Properties declare two accessor methods – the setter and getter. This is done automatically for you, but let’s look under the hood so we can understand what’s going on.

In the ‘get’ accessor or getter, all we’re doing is returning the synthesized instance variable. We don’t create a new object using “alloc”, “new”, “copy”, or “mutableCopy” so we don’t have to retain or release it.

- (NSString *)string {
     return _string;

In the ‘set’ method or setter, the value we assign could be disposed of at any time, so we have to take ownership of the object to make sure this won’t happen.

- (void)setString:(NSString *)newString 
   [newString retain];
   [_string release];
   // Make the new assignment.
   _string = newString;

We take ownership of the newString object using retain, relinquish ownership of the old string using release and then make the new assignment.

We can use these accessor methods and our MRR rules to make sure we manage our memory properly. A final example:

- (void)change 
   NSString *anotherString = [[NSString alloc] 
                             initWithString:@"some string"];
   [self setString:anotherString];
   [anotherString release];

We create anotherString with alloc so we balance it at the end with release. We also use our setter accessor method to set the value of our string property. Within our setter methods we take ownership of any relevant objects and release it when we don’t need, making sure we manage our memory effectively.

Ownership Policy

Using retain and release to manage our memory uses a model called reference counting. Each object has a retain count and this is used to keep track of ownership.

  • When you create an object, it has a retain count of 1.
  • When you send an object a retain message, its retain count is incremented by 1.
  • When you send an object a release message, its retain count is decremented by 1.
  • When you send an object a autorelease message, its retain count is decremented by 1 at the end of the current autorelease pool block.

Once an object’s retain count is reduced to zero, it is deallocated.

That should wrap up the basics of memory management using the manual-retain-release model. An understanding of this forms the foundation for building upon ARC, which we’ll tackle in our next post in the series.


Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Get Started

7 Responses to “iOS Memory Management: The Manual-Retain-Release Model”

  1. 激安 ブランドスーパーコピー新しいものを販売しています。ルイルイヴィトンコピー、グッチコピー、シャネルコピー、ブランドコピー、ブランドスコピー、ブランドコピー時計などルイヴィトンコピー 激安 ブランド、スーパーコピー、代引き対応、レプリカ、安心通販ルイヴィトン偽物、シャネル偽物、グッチ偽物、エルメス偽物、クロエ偽物、カルティエコピー、オメガコピー、IWCコピー楽天ヴィトンコピー屋 http://www.gginza.com/bag/louisvuitton/index.html

  2. 人気スーパーコピーブランド時計激安通販専門店私達は長年の実体商店の販売経験を持って、先進とプロの技術を持って、高品質のスーパーコピー時計づくりに 取り組んでいます。最高品質のロレックス時計コピー、カルティエ時計コピー、IWC時計コピー、ブライトリング時計コピー、パネライ時計コピー激安販売中商品の数量は多い、品質はよい。海外直営店直接買い付け!★ 2015年注文割引開催中,全部の商品割引10% ★ 在庫情報随時更新! ★ 実物写真、付属品を完備する。 ★ 100%を厳守する。 ★ 送料は無料です(日本全国)!★ お客さんたちも大好評です★ 経営方針: 品質を重視、納期も厳守、信用第一!税関の没収する商品は再度無料にして発送します http://www.msnbrand.com/brand-copy-IP-1.html

  3. スーパーコピー時計専売店当店は海外安心と信頼のスーパーコピーブライトリング、代引き店です.正規品と同等品質のシャネル コピー代引き,品質が秀逸,値段が激安!ブライトリングコピー,代引きなどの商品や情報が満載!全商品写真は100%実物撮影です! お客様の満足度は業界No.1です!スーパーコピー時計,時計コピー ,ブランド時計コピー販売(n級品)店舗 ブランド腕時計(ロレックス,ブライトリング,タグホイヤー,オメガ,ガガミラノなど)の最新 情報やイベントを紹介する正規販売店と腕時計コピーの専門サイトです。当店はロレックスやパテックフィリップなどの新品スーパーコピー時計の販売と。 http://www.msnbrand.com/brand-copy-IP-4.html

  4. 最も美しいスーパーコピー,超格安ブランド時計コピー激安通販専門店!ブランド品に憧れて、予算オーバーし、諦めなければならないことがありましたか。スーパーコピー品が一番いいチョイスだ思います。少しのお金でも、世界中一番人気を持つブランド品、貴族しか買えない贅沢品がゲットできます。素敵な外観と実用性に優れたブランド コピー バッグ,,スーパーコピー財布,スーパーコピー 代引き,エルメス コピー,ルイヴィトン 財布、ルイヴィトン バッグ、ベルトなどの逸品はお客様に提供します。誰でもブランド品を手に入れられるのは弊社の経営理念です。当店の最大目標はお客様が安心してお買い物が出来き、お客様の頼りになるお店を目指す事ことです。お洒落な貴方はきっとこのチャンスを見逃しなくよ! http://www.bestevance.com/RogerDubuis/index_3.html

  5. エルメス ボリードスーパーコピー.ブランド直営店.ブランド,エルメス激安通販,業界で最高な品質に挑戦します!”ブランドN級品ブランドコピー 代引き,スーパーコピー時計,ブランドN級品,楽天コピーブランド,,偽物ブラン日本最大級の最高のスーパーコピーブランド財布激安代引き販売店,スーパーコピー時計の激安老舗.!国内No.1時計コピー工房,アフターサービスも自ら製造したスーパーコピー時計なので、技術力でお客様に安心のサポー トをご提供させて頂きます。スーパーコピー 代引きN品をご 購入の方は、こちらへ.弊社は正規品と同等品質のコピー品を低価で お客様に提供します!すべての商品は品質2年無料保証です。100%実物写真ですし、品質が完璧です!”スーパーコピーブランド財布激安 偽物財布激安コピー ルイヴィトン財布偽物,偽物財布コピー http://www.bagkakaku.com/celine_bag.html

  6. 2015ブランド財布コピールイヴィトン財布コピー,シャネル財布コピー,グッチ財布コピー,エルメス財布コピークロエ財布コピー,ブラダ財布コピー,ブルガリ財布コピー,ドルチェ&ガッバ―ナ財布コピーバレンシアガ財布コピー,ボッテガ.ヴェネタ財布コピーロレックス時計コピー,ブルガリ時計コピー,フランク ミュラー時計コピーシャネル時計コピー,カルティエ時計コピー_オメガ時計コピー,IWC時計コピールイヴィトン時計コピー,オーデマ ピゲ時計コピー,ブライトリング時計コピーコピーブランド、ブランド激安、人気ブランドの販売、通販、オークション、新作のスーパーコピーブランドコピー、韓国スーパーコピー、ブランド激安、偽物ブランド、ブランドバッグ、激安かばん、ルイヴィトン偽物、財布激安.商品は全く写真の通りです。 http://www.newkakaku.net/lb1.htm

  7. 日本最高級スーパーコピーブランド時計激安通販専門店,高品質時計コピー,2015最新作、国際ブランド腕時計コピー、業界唯一無二.世界一流の高品質ブランドコピー時計,当店はスーパーコピー時計専門店,販売以下世界一流ブランドコピー時計:ロレックスコピー、ウブロコピー、オメガコピー、シャネルコピー…ンプルに見えて目を奪われてしまう独創的なブルガリのラインアップです。1884年ブルガリの創始者ソティリオ?ブルガリが銀細工師の一族としてイタリ アにオープン。ブルガリ?ブルガリシリーズ。古代ローマの円形競技場をモチーフにした時計「アンフィテアトロ」、若い世代向けの腕時計「ソロテンポ」を発 表。2000年には新会社ダニエル?ロード&ジェラルド?ジェンダ オート?オルロジュリー社を設立しました。本物ブランド時計に間違える程のスーパーコピー時計通販!スーパーコピーは業界n級品最高品質に挑戦!ロレックスコピー,パネライコピー,ウブロコピー,オメガコピー,ルイ?ヴィトンコピー,エルメスコピーを初め世界中有名なスーパーコピーブランドを激安で通販しております!HERMES(バッグ、時計) CHANEL(バッグ、時計)LOUIS VUITTON(バッグ、時計) BVLGARI時計Christian Dior(バッグ、小物) COACH(バッグ)GUCCI(バッグ、小物) ROLEX(時計)OMEGA(時計) IWC(時計) http://www.bagkakaku.com/louisvuitton_bag_5.html

Leave a Reply

You must be logged in to post a comment.

man working on his laptop

Are you ready to start learning?

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Start a Free Trial
woman working on her laptop