Basic memory management in Objective-C
First of all what is memory management?
Memory management is the act of managing computer memory. In its simpler forms, this involves providing ways to allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed.-wikipedia
Working with memory management in Objective-C is kind of challenge for me because I am used to work with programming language that has auto garbage collection such as PHP and Java. In this post I will share some of the basics I have learned with my first project, I assume that the reader has some basics with Objective-C.
Lets start with the general rule in memory management in Objective-C
You are only responsible for the objects you own!
Being responsible means you have to throw away objects that aren't needed anymore, So how do you own an object? there is a good acronym (N.A.R.C) which help determine if you own the object and responsible for it.
N corresponds to "new" keyword
A corresponds to "alloc" keyword
R corresponds to "retain keyword
C corresponds to "copy" keyword
this keywords are all related to instantiating an object from a class which hints us that you are responsible for that object you have created, lets say for example you want to instantiate SomeObject class on a variable named someObj.
SomeObject * someObj = [SomeObject new];
This object created using the new keyword must be released, In order to that we must call the release method of someObj.
[someObj release];
NOTE: All objects in Objective-C is inheriting from NSObject which has the method of release that we can call.
There is an alternative way of instantiating an object it is the use of alloc and init* keyword.
SomeObject * someObj = [[SomeObject alloc] init];
[someObj release];
NOTE: There is a difference when using the new keyword and the alloc init combination when instantiating an object. new keyword just allocate the object, while the other one will allocate the object and at the same time initialize it with values just like the constructor in other language.
Next up is retain keyword. All objects has its retain count and ability to increase it, the retainCount method of the object tells how many references an object has. We must release every references if we don't need them anymore.
SomeObject * someObj = [[SomeObject alloc] init];
[someObj retain]; // increment the retainCount by 1 a total of 2
/* the total retain count is 2 we need to decrease it to 0 in order to throw the object */
[someObj release]; //we need to release it retainCount = 1;
[someObj release]; // release it again retainCount = 0;
There are times when you need to copy the same object to another variable these objects that are copied must also be released at some point in the application. You can copy an object using the copy keyword.
SomeObject * someObj = [[SomeObject alloc] init];
SomeObject * copiedObj;
copiedObj = [someObj copy];
[someObj release];
[copiedObj release];
In conclusion these rules are some of the basics of memory management but I didn't tackle yet some questions like when to release an object, when to retain an object etc. You must also take note that you can't release an object yet if you need it because if you do release the object and access it your program will definitely crash, I'm kind of new here so any comments and suggestion is deeply appreciated I will write follow-up to this post if I have a free time please do follow me at twitter sbalbalosa thank you!













