views:

195

answers:

2

I've been learning to develop applications on the cocoa touch platform. I've come a long way but one piece that I can't wrap my head around is memory management.

I thought I'd take a route of comparison rather than trying to start from scratch. I can't find much on the difference between the CLR (.net) and Objective-C 2.0 so I'm wondering if the stack overflow community can help me with this question.

What are the key/important differences in memory management between Objective-C 2.0 and the CLR? With regard to Objective-C 2.0, I'm developing on the iPhone OS and the autorelease functionality is discouraged.

I guess I'm looking for a comparison of the two... Since I'm coming from a .NET background, what might be things I need to know about Objective-C 2.0 memory management?

Thank you all!

+1  A: 

On the iPhone it is manual. If you allocate or retain memory you must release it.

In the .NET CLR you don't care how memory is or what object do with it for the most part.

For the iPhone you need to balance out every object allocation, init or retain with a release. As the iPhone uses a simple reference counting mechanism, once the count falls to 0 the object will get cleaned up.

One thing that I needed to get used to is you can have a property that automatically does a retain, so if you create an object then assign it to the property it will have a count of 2. What works better is create the object to a temp variable assign to the property, then release the temp variable to get the count down to 1.

Darryl Braaten
+1  A: 

The CLR runs in a virtual machine; all the deallocation of objects are handled by the garbage collection system. Generally in Objective-C memory must be managed manually via either old C style malloc/free or via the reference counting system of retain/release. If you come from a standard "C" background, the technique won't seem too foreign.

With reference counting, the system counts how many times a specific object is used - this is basically the "retain" mentioned above. When something is done using an object the object is manually send a "release" message which decrements the object's retain count by 1. When the count reaches 0, the system automatically deallocates the object. This will seem extremely cumbersome compared to the CLR/.NET but that mechanism provides better performance and more control.

If you are coding in Objective-C 2.0 on the Macintosh, you are in luck since garbage collection can be enabled via an option in XCode. This will be closer to what the CLR provides. If you are developing on the iPhone, garbage collection costs too much in terms of memory and CPU so its not an option. Memory must be managed manually.

Fortunately, there is an in-between option that is commonly used by sending and "autorelease" message to an object. This mechanism, included in both Macintosh and the iPhone, basically pools allocated objects into a global dictionary (its actually called an autorelease pool). Either when the application exists or when the pool is drained, the objects are then de-allocated. However, not everything goes into the autorelease pool nor do you want to put everything there. I recommend the dry but important reading of the Objective-C 2.0 Programming Language from the Apple site that goes into way more detail.

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

Good luck and have patience. Objective-C isn't a new kid on the block like .Net (its over 25 years old) but it has some incredible features that .Net is just now starting to incorporate.

Mr. Cairo