Saturday, July 13, 2013

Memory management in Objective - C


Bonjour les amis,

 
This post explains the memory management in Objective-C .
Amigos with C++ background can also refer to this post as it will help them to get a basic idea of how the memory is allocated  and should be handled by you. If you have worked or working on any in house frameworks, this post will help you to get some overview of how memory handler module works in your  your framework.

Before going any further I would first like to explain the "
Brique de fooundation" of memory management in C++, Reference counting. Consider two programmers working on two different classes but using same reference of any object xyz. Now who will delete the memory allocated to this object? If programmer1 delete the object and programmer 2 is still using it he will get a crash. Now how the programmers will know when to delete the object and who will delete it ? Now consider the same situation in a large project where many programmers are working on different classes and they all uses many such object's reference in different classes!!!! problemas? Of-course it's a big problem!!! To solve this issue reference counting was engineered.


What is reference counting ?


Definition from wiki : 
In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource.

In layman language a reference count is an internal count managed by an object, indicating the count that someone has its ownership.

What is ownership:  An owner of an object is the code snippet which states that "it needs that particular object and no one can delete it".

Methods/Messages related to memory management in Objective- C

alloc (also called "new") : This will create a new object and claims an  ownership of it.

      alloc will explicitly creates an instance of an object. The piece of code creating object using alloc is the only whole soul owner of that object and should delete that object when it has done with it. This will increase the retain count of that object by one. 


retain : This will claim an ownership of an existing object      retain will increase the retain count (reference count) by 1 and makes the calling piece of code a new owner of that object. So some one using retain can say I am also the owner so no one should delete the object till I am done with it . Here also the object should be deleted once its usage is over.


copy : Creates a copy of an object and claims an ownership of it 

    copy will create a copy of the existing object and makes the calling piece of code the owner of that copy of object. So this copy will manage the separate track of retain count. And this copy of object should be deleted when its usage is done.

release : This will delete the object and take away the ownership.

    release will will delete the object decreasing its reference count by 1. But remember "only owner should use it".
 
autorelease :
This will delete the object automatically and don't give the ownership.
    with autorelease  you don't lock the object but you be the temporary owner of the object such that it is deleted automatically when after sometime its usage is over. All the objects are kept in a memory pool called autorelease pool ( an instance of NSAutorelease class) and are deleted at once like  a garbage collector when the pool cleaned. The reference count is made 0 when the objects are deleted.
 
   ARC ("automatic reference counting") is the  the garbage collector mechanism in Objective - C  . All new projects generally uses ARC to prevent the overhead of manual memory management.