Search This Blog

Thursday 17 February 2011

Iphone Memory Management

In Iphone, memory is one of the critical issues.Objective-c does not support garbage collection like java and other object oriented languages.We have to take care while creating the object and freeing the object from the memory.
Here we have two scenarios.
1. In iphone Objects are created using alloc,new,copy. If we use any one of the methods we have to release it. That means, we are creating an object and acquiring the object ownership.Means we are the owner of the newly created object.So it is our responsibility to release(free) the newly created object.Whenever we release the object it is deallocated from the memory. And that released memory is used by the other object in future.

Ex:

MyClass *object = [MyClass alloc]init]];//count 1
[object retain];//count 2
[object release];//count is decremented by 1 now count=>2-1=1

[object release];//Again count is decremented by 1 now count=>1-1=0

Here in the first line we have created an object with count 1.Then in the second line we retain the object, means we have owned that object by increasing the count to 2. In the third line we released the object then the count decreased to 1, in the fourth line again we released the object,then the count becomes zero(0). Whenever the count becomes zero the object allocated in the memory will be deallocated.

Here is an interesting one, In the above example we have created an object using alloc,new,copy, means we are the owner of the created object,we have to nurish it. But the worst case is, if we forget to release the object what will happen?.

can a newbie guess?
No.

If we forget to release the memory,the object stays in the memory, eventhough the program is terminated. Number of applications are running in our iphone,if all the applications forgot to release the memory what will happen?. Huge amount of memory is wasted.This type of scenario is known as a leak or memory leak.

That is why i mentioned that memory management is critical.In order to overcome the memory leaks.We have to release the object whatever we own.

And the second scenario is

NSArray *array = [NSArray arrayWithObjects:mainSprocket,
auxiliarySprocket, nil];


Here in the above example we have not used alloc,copy,new.Means we dont own the newly created object.We dont have privilege to release the object.Whenever we create objects like this, these objects are autoreleased.Means these objects references are stored in the autorelease pool.Whenever the application is terminated the objects are destroyed.We will discuss about the autorelease later.

Whenever we are creating object using init,alloc,copy,mutablecopy we have to release it.Else there is no need to release it.

The terms involed in the memory management:
1.alloc
2.init
3.dealloc
4.autorelease.

All these methods are available in the super class NSObject. In objective c, NSObject is the universal class.

alloc:This method is used to allocate the memory for the object.

init:This method is used to initialise the instance variable of the object just like constructors in the java and c++.

Note:
We have to use both alloc and init in the same line,

MyClass *obj = [[MyClass alloc]init];

If we do so object is created and initialized.

MyClass *obj = [Myclass alloc];
[obj init];

In the first line object is created, in the second line object is initialised and not returns the required object initialization.

So that is why we have to use both the alloc and init in the same line.

dealloc:

This method is used to free the memory.But we should not call the method directly. Whenever the object count becomes zero, this method is called and the object in the memory will be deallocated. First of all it will call its super class dealloc method.[super dealloc];.

autorelease:

MyClass *object = [[MyClass alloc]init];//count=1
//do something here

[object autorelease];//Here the count will become 0,but not know, it will be in the future.

Here the scope of the object remains in the memory until the application get terminated. In this situation NSAutoRelease pool object will take care in releasing the object.

Whenever we use autorelease, this object will be stored in the autorelease pool.

***In the main method NSAutoReleasePool object is automatically created by the Xcode.This object will hold the refereneces of the objects that are autoreleased.

int main(....)
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc]init];

int x = UIApplicationMain(.......);
[pool release];

whenever the applicatin get terminated.The pool will be released,before that the autorelease pool first makes the object count of the objects in the pool to zero.Whenever the objects count is zero dealloc method is called and the memory is freed.

Also read the follow document

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html