I would like to start my blog talking about memory management in iPhone. When you develop application on desktop, we don’t care much about this problem because of the huge physical memory on desktop nowadays. But in mobile devices, that will be a headache problem, especially when user run multiple programs in the background.
To handle this kind of problem, that will be easy if you keep in mind some basic concepts:
- Object ownership: if you create a object with alloc, copy, you will need to release the memory of that object. Otherwise, you don’t need to release.
Example 1:
ClassA * classA; classA = [ClassA alloc] init]; //do business logic with classA ... [classA release];
In this example, classA object has been created and a physical memory holding the value of classA. You must release that object if you don’t need it anymore. The example above is the same with this:
Example 2:
ClassA * classA; classA = [[ClassA alloc] init] autorelease]; //do business logic with classA ...
In first example, you are the owner of the object (classA), you must release it when you don’t need it anymore. But now you use “autorelease”, that means this object will be controlled by auto release pool. When you go out of the method using that object, the pool will release the memory of that object for you. You don’t need to release the object yourself.
Example 3:
ClassTest * varA = [ClassTest alloc] init]; ClassTest * varB = [varA copy]; //do business logic with varA, varB ... [varA release]; [varB release];
In this example, you create object varA, and you copy the value of varA to varB. That mean you own 2 objects and you need to release them after using.
- Accessor method: whenever you define a class, of course you will define instance variables. With assign, retain, copy, which one you should use for your object?
Assign: you should you this with primitive variables such as int, float, NSInteger,… If you use with your object (such as NSString), the setter with be:
- (void) setString: (NSString*) newStr [
string = newStr;
}
The object is not retained, so it will be deallocated even you are using it.
Retain: the correct answer for above problem is you should use retain property. With retain, your object will be retained after the setter method.
- (void) setString: (NSString*) newStr {
[string release];
string = [newStr retain];
}
Using this way, you will use one object within this class. I usually use this way to use one object in my application, between many view.
Copy: this way will make a copy of other object and assign that to your object. That means you will have 2 physical memory contain 2 objects with same values.
- (void) setString: (NSString*) newStr {
if (string != newStr) {
[string release];
string = [newStr copy];
}
}
Important: you should release your object in dealloc method when you using retain or copy. If not, that will cause memory problem.
Conclusion:
The concept I describe above is simple, I think it is suitable for new developers to be familiar with Objective-C. There will have some more topics about memory management, memory optimization in the futre. If you have any ideas, you are welcome to leave a comment. I am very appreciated that you share your ideas with me.
References:
Memory Management Programming Guide – Apple