In any platform with garbage collection enabled, you don't really have to worry about memory management. Developers who come from a garbage collected environment like Java will specially find it difficult to understand the concepts of memory management. Memory management is probably one of the toughest obstacles that a beginner might face while learning Objective C. Since there is no garbage collection facility in iphone, any wannabe iphone developer must learn about it. So one question might pop up in your mind. Why is it so important? Because iphone has limited memory and you don't want to burn its memory out so that your application just crashes out. Now lets get to the basic concepts of memory management.
Object Ownership
There are certain conditions when you'll become the owner of an object. There are 3 ways by which you can become an owner of an object. But remember you have to dispose all objects you own.
Case 1. You created a new object in the memory using alloc.
e.g. NSString *string1 = [[NSString alloc] initWithString:@"hello"];
Case 2. You copied an existing object to another object.
e.g. NSString *string2 = [string1 copy];
Case 3. You retain an existing object.
e.g. [string2 retain];
In case 1, you used alloc to create an object so a new object named 'string1' is created. In case 2, you copied string1 to string2. In case 3, you retained an already existing object 2. So you have ownership of string1 and double ownership of string2.
Any object that is created using any other method is not owned by you.
e.g. NSString *string3 = [NSString stringWithFormat:@"%d",12345];
Although it seems as if you just created a string object, you don't own it.
Object Disposal
You have to dispose every object that you own. You dispose an object by sending a release or an autorelease message. To dispose the objects from the above 3 cases we have to do the following:
Case 1: [string1 release];
Case 2: [string2 release];
Case 3: [string2 release];
The difference between release and autorelease is that autorelease releases the object only after certain events are processed whereas release will release the object instantly.
Retain Count
Internally Objective C uses the concept of retain counts for memory management. Every object has a retain count. Retain counts can increase and decrease. When the retain count of any object becomes zero, it is released from memory. It means that the object is no longer available. Trying to access a released object will cause the application to crash. Here are the retain count rules:
1. When you create an object with alloc or copy, it has a retain count of 1.
2. When you send an object a retain message, its retain count is incremented by 1.
3. When you send an object a release message, its retain count is decremented by 1.
4. When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future.
5. If an object’s retain count is reduced to 0, it is deallocated.
eg: NSString *string = [[NSString alloc] initWithString:@"hello"]; => retain count = 1
[string retain]; => retain count = 2
[string release]; => retain count = 1
[string release]; => retain count = 0(object released or disposed)
Wednesday, May 26, 2010
Subscribe to:
Post Comments (Atom)
That was nice info sir ;)
ReplyDeleteawsum stuff!! key things for Memory Management.
ReplyDeletethanx
gud info.....
ReplyDeleteAlso, check GarbageDisposerReviews.com
ReplyDelete