Why people have difficulty in understanding memory management in Objective-C\Cocoa

With introduction of the Objective-C 2.0, many people were relieved from headache of managing memory. The garbage collector of Obj-C 2.0 is very easy to use and people can forget about if certain object should be retained or released, and so on. Yes. The garbage collector is good. However I still write with retain and release method. Why? Because I can control memory space! I can get rid of memory space which is not used any more very quickly.

For group projects in which there are some people who don’t understand memory management well, the garbage collector is very good choice to adopt. However, one solution can’t be answers for all problems. So, old retain, release, autorelease are still necessary.

Then, why there are people who don’t understand mechanism of object retention? Probably one reason is when objects are released is different from when an object is created with convenience methods and general -alloc and -init… methods.

For example, an object created with convenience method will have its retain count as a value 1. You can use this object until the variable which point to this object is still in the scope. However, if it is out of scope, it will be a candidate for removal. So, if you want to keep this kind of object without worrying about the scope, you should retain it. Then its retain count becomes 2, and after using it completely, you release it. Then its retain counter becomes 1 and it becomes a candidate for removal by so-called semi-garbage collector, or the old pool-based memory management mechanism.

On the other hand, an object created with -alloc and -init method doesn’t become a candidate for removal when its retain count is 1. It is up to you and up to the release method call. So, you can keep your object which is out of scope even when the retain count is 1. The reason is that pool doesn’t manage it. So, it doesn’t autorelease it. Only explicit release call can dispose the object. So, you don’t need to call the retain method if another object or something else also doesn’t want to keep it.

It is very simple!

2 responses to this post.

  1. Posted by seachicken on August 31, 2009 at 4:17 AM

    It seems little bit confusing. Do I get instances with retain count value 1 for both created by alloc and convenient method?

    Reply

    • Posted by jongampark on August 31, 2009 at 8:18 AM

      Yes. You can always check the retain count by calling retainCount method of NSObject.
      Why is it confusing?

      Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: