Memory management is more critical on handheld devices as they have very less memory as compared to desktop systems
Memory crunch can cause an application to crash, can also put device to hang.Most of the languages comes with a garbage collector which collects memory which is unused, and put back as a reusable chunk of memory.
Example: Java has a inbuilt garbage collector. which cleans up the unused variables and objects.
In iPhone a programmer has to do the task of garbage collector ie cleaning of unused variables, objects.If unused objects are not cleaned then the program will starting leaking memory and eventually cause the application to crash.
Few concepts to keep in mind while doing memory management
- Object Ownership
- Initializing Object
- Releasing Object
- Retaining Object
Memory management works:
- retainCount property of NSObject class.
- Keeps track of references for an object.
- release and retain methods of NSObject class helps in memory management.
John takes ownership of an object if John creates it using a method whose name begins with “alloc” or “new” or contains “copy” (for example,alloc, newObject, or mutableCopy), or if John sends it a retain message.
Example: MyClass *c=[[MyClass alloc] init];
- Every Object has a retain Count.
- Code maintains the retain count.
- When retain count reaches 0, Objective C deletes the object.
- Alloc, retain, new , copy methods increase the object’s retainCount by 1.
Example:
[xObj alloc]// retain count increased by 1
[xObj retain] // retain count increased by 1
[xObj alloc]// retain count increased by 1
[xObj retain] // retain count increased by 1
- release and autorelease decrease the retain count by 1.
Example:
[xObj release] // decreases the retain count by 1
[yObj autorelease] // decreases the retain count by 1 at some point of time in future
[xObj release] // decreases the retain count by 1
[yObj autorelease] // decreases the retain count by 1 at some point of time in future
Steps:
MyClass *myClass=[[MyClass alloc] init];
retainCount =1
MyClass *myClass=[[MyClass alloc] init];
retainCount =1
- [myClass retain];
retainCount=2
- [myClass release];
// decrease the retain count by 1
- [myClass release];
// decrease the retain count by 1
retainCount is 0 now.
- [myClass release]; // crash
Rule of Thumb:
- If you create an object, make sure you release it.
- Balance the retainCount by retain and release methods.
- In this memory management happens automatically.
- Facilitates releasing the objects that het passed around from method to method.
- List of objects to send release message later in time.
- [myClass autorelease] adds object myClass to autorelease pool
- Autorelease pool is always emptied at the end of current event that app is processing.
Few examples:
Properties retain and release
Example:
@property (nonatomic, retain) NSString *str;
@synthesize str;
Generates …..?
-(void) setStr: (NSString) *x
{
if(x!=str)
{
[str release];
str=[x retain];
}
}
- (NSString) str{
return str;
}
All this memory management is done behind the scenes.Not visible to programmer.
Other concept:
- Dealloc also known as destructors.
- Class doing memory management will have this method.Releases all the properties and internal state of objects.
- Must release instance objects.
- Have to call the dealloc of super class.
- (void) dealloc {
[str release];
[super dealloc];
}
[super dealloc] is required otherwise an object will never be deleted even if the retain count is 0.
Every property and instance variable should be released in this method.
Memory Bugs
* release to objects already deleted
Result: crash
Program received signal: “EXC_BAD_ACCESS”
* forget to send release
Result: Memory Leak
No comments:
Post a Comment