12_Obj-C Memory Management

Memory management is one of the most important process in any programming language.

Objective-C Memory management techniques can be broadly classified into two types.

  • “Manual Retain-Release” or MRR
  • “Automatic Reference Counting” or ARC

The only difference between MRR and ARC is that the retain and release is handled by us manually in former while its automatically taken care of in the latter.

Manual Retain-Release

  • In MRR, we explicitly manage memory by keeping track of objects on our own.
  • This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment.

The memory life cycle of the Class A object is shown in the above figure.

As you can see, the retain count is shown below the object, when the retain count of an object becomes 0, the object is freed completely and its memory is deallocated for other objects to use.

  • Class A object is first created using alloc/init method available in NSObject. Now, the retain count becomes 1.
  • Now, class B retains the Class A’s Object and the retain count of Class A’s object becomes 2.
  • Then, Class C makes a copy of the object. Now, it is created as another instance of Class A with same values for the instance variables. Here, the retain count is 1 and not the retain count of the original object. This is represented by the dotted line in the figure.
  • The copied object is released by Class C using the release method and the retain count becomes 0 and hence the object is destroyed.

In case of the initial Class A Object, the retain count is 2 and it has to be released twice in order for it to be destroyed.

This is done by release statements of Class A and Class B which decrements the retain count to 1 and 0, respectively. Finally, the object is destroyed.

MRR Basic Rules

  • We own any object we create
  • We create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy”
  • We can take ownership of an object using retain
  • A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker.

We use retain in two situations

  • In the implementation of an accessor method or an init method, to take ownership of an object we want to store as a property value.
  • To prevent an object from being invalidated as a side-effect of some other operation.

When we no longer need it, we must relinquish ownership of an object we own: We relinquish ownership of an object by sending it a release message or an autorelease message.

Example of retain and release

note : [object retain] ; Basically it is used to take ‘ownership’ on an object, i.e. by calling retain, the caller takes the responsibility of handling memory management of that object.

“Automatic Reference Counting” or ARC

In Automatic Reference Counting or ARC, the system uses the same reference counting system as MRR, but it inserts the appropriate memory management method calls for us at compile-time.

We are strongly encouraged to use ARC for new projects.

If we use ARC, there is typically no need to understand the underlying implementation described in this document, although it may in some situations be helpful.For more about ARC, seeĀ Transitioning to ARC Release Notes.

It uses the retain and release operations internally making it easier for the developer to code without worrying about these operations, which will reduce both the amount of code written and the possibility of memory leaks.

Here is a simple ARC example. Note this won’t work on online compiler since it does not support ARC.

Example of ARC

When we compile the above program, we will get the following output.

Hello, World!
Object deallocated




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s