Collect your own Garbage! (Objective C iOS platform)

I am my own garbage collector. I have to do it both in my house and when developing in Objective C (iOS platform) (seems like i can't escape this at work and home). I have created a prototype of an iPad application and i have to crash and learn Objective C.I have a strong background in Java which pampers developers with its automatic garbage collection even though you can force garbage collection in Java rest assured that it is there in the background. It is whole different story for Objective C for the iOS platform. This is probably one of the reasons why Java is successful among the language that gets its DNA from the C, C++ language.

Object Initializer

In Java you use the "new" keyword to create Objects. One step and your finish. In Objective C it is a two step operation you have to allocate and initialize ("alloc" and "init" respectively). The "alloc" makes the space in the heap for class' instance variables. The "init" initializes the object. This is where you default the values for your instance variables. It is possible to separate the "alloc" to the "init" in different line of a program but it is not wise to do so.


Dealing with Objects


In Java if you get an object reference you know it will be garbage collected once you assign "null" to the variable holding the reference to that object or if the object is not referenced by any variable.
Since there is no garbage collection who frees the memory for all these objects ? (The objects that you allocated and initialized and the objects that you get reference for) . The simple answer is "You". So how will you manage objects ? (specially it's deallocation). I have been looking around for an answer to this and it is by following a strict set of simple rules when your coding.


Reference Counting


Reference counting is just a simple set of rules that you need to follow to avoid memory degradation because of objects that isn't cleaned up.

1. Take ownership for an object you want to keep a pointer to

Usually done by sending the "retain" message to NSObject

2. When your done with the object give up the ownership

Usually done by sending the "release" message to NSObject

3. So when no one claims or take ownership of the object it gets deallocated

This is getting rid of the object. Any message sent to a deallocated object will crash your program.


There is a way of having "temporary" ownership of an object instead of using "retain" you can do an "autorelease" which will give up the object after or any future time but not before the current event is finished. Somebody else can "retain" the object if they want to if you do an "autorelease".


This is important. -> You have to make sure that you just "release" objects that you own because doing a release to an object pointer will release objects that you might not own or somebody else is using which can give rise to problems.


I hope this gives a good overview on garbage collecting in Objective C for the iOS platform.

Ciao!

Comments

Popular posts from this blog

OAuth 1.0a Request Signing and Verification - HMAC-SHA1 - HMAC-SHA256

Spark DataFrame - Array[ByteBuffer] - IllegalAurmentException

Gensim Doc2Vec on Spark - a quest to get the right Vector